Android ViewSwitcher控件

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" />

调用ViewSwitchershowNext()showPrevious()来实现视图的切换。
在这里插入图片描述

2. setFactory设置视图

ViewSwitchersetFactory(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%" />

效果如下
在这里插入图片描述

相关文章
Android ViewSwitcher控件
Android ViewFlipper类

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值