1. 简单用法
ViewSwitcher
继承ViewAnimator
,主要用于视图的切换。
ViewSwitcher
重写了addView(View, int, ViewGroup.LayoutParams)
方法,使其子控件不超过2个。
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (getChildCount() >= 2) {
throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
}
super.addView(child, index, params);
}
通过配置属性指定切换动画
android:inAnimation
指定进入时动画android:outAnimation
指定退出时动画
配置文件
<ViewSwitcher
android:id="@+id/view_switcher"
android:layout_width="match_parent"
android:layout_height="200dp"
android:inAnimation="@anim/anim_enter_from_bottom"
android:outAnimation="@anim/anim_exit_to_top">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/switcher1"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/switcher2"
android:scaleType="fitXY"/>
</ViewSwitcher>
进入动画anim_enter_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%"
android:toYDelta="0"
android:duration="1000" />
退出动画anim_exit_to_top.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:toYDelta="-100%"
android:duration="1000" />
调用ViewSwitcher
的showNext()
和showPrevious()
来实现视图的切换。
2. setFactory设置视图
ViewSwitcher
中setFactory(ViewFactory)
方法设置了子视图,调用obtainView()
方法添加了两个子控件。
public void setFactory(ViewFactory factory) {
mFactory = factory;
obtainView();
obtainView();
}
private View obtainView() {
View child = mFactory.makeView();
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp == null) {
lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
addView(child, lp);
return child;
}
添加自定义ViewFactory
,返回ImageView
viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView iv = new ImageView(ViewSwitcherFactoryActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new FrameLayout.LayoutParams(-1, -1));
return iv;
}
});
3. 多个视图切换
有多个视图需要时,需要自定义next()
和previous()
方法。
private ViewSwitcher mViewSwitcher;
private int mPosition = 0;
private int[] mResIdArray = {
R.drawable.switcher1,
R.drawable.switcher2,
R.drawable.switcher3,
R.drawable.switcher4,
R.drawable.switcher5
};
private int mCount = mResIdArray.length;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... ...
((ImageView) mViewSwitcher.getCurrentView()).setImageResource(R.drawable.switcher1);
}
next()
和previous()
方法
private void next() {
++mPosition;
setSelection((ImageView) mViewSwitcher.getNextView());
mViewSwitcher.setInAnimation(this, R.anim.anim_enter_from_bottom);
mViewSwitcher.setOutAnimation(this, R.anim.anim_exit_to_top);
mViewSwitcher.showNext();
}
private void previous() {
--mPosition;
setSelection((ImageView) mViewSwitcher.getNextView());
mViewSwitcher.setInAnimation(this, R.anim.anim_enter_from_top);
mViewSwitcher.setOutAnimation(this, R.anim.anim_exit_to_bottom);
mViewSwitcher.showPrevious();
}
private void setSelection(ImageView imageView) {
if (mPosition < 0) {
mPosition = mCount - 1;
} else if (mPosition >= mCount) {
mPosition = 0;
}
imageView.setImageResource(mResIdArray[mPosition]);
}
进入动画anim_enter_from_top.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="-100%"
android:toYDelta="0"
android:duration="1000" />
退出动画anim_exit_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:toYDelta="100%" />
效果如下