Android界面编程之ViewSwitcher

ViewSwitcher:
ViewSwitcher可以用factory提供视图,也可以自己添加,最多只能有两个子视图,且任何时候只能显示其中一个。
如此,可以猜测到该控件的工作过程:
(1)为ViewSwitcher指定factory对象
(2)为ViewSwitcher的视图提供布局
(3)让ViewSwitcher翻页(按钮或者触屏滑动)
这里写图片描述
示例:实现图片切换
这里写图片描述
布局

<ViewSwitcher
        android:id="@+id/viewSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <Button
        android:id="@+id/butten_prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一页"
        android:onClick="prev"
        app:layout_constraintTop_toBottomOf="@+id/viewSwitcher1"/>
    <Button
        android:id="@+id/butten_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页"
        android:onClick="next"
        app:layout_constraintTop_toBottomOf="@id/viewSwitcher1"
        app:layout_constraintLeft_toRightOf="@id/butten_prev"/>

MainActivity:

public class MainActivity extends AppCompatActivity {
    private ViewSwitcher viewSwitcher;
    private int[] images = new int[]{R.drawable.img1,R.drawable.img2,
            R.drawable.img3,R.drawable.img4,R.drawable.img5};
    private int index = 0;
    private LayoutInflater inflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewSwitcher=(ViewSwitcher)findViewById(R.id.viewSwitcher1);
        inflater = LayoutInflater.from(this);
        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getApplicationContext());
                return imageView;
            }
        });
        next(null);
    }
    public void prev(View view){
        viewSwitcher.setInAnimation(this,R.anim.slide_in_right);
        viewSwitcher.setOutAnimation(this,R.anim.slide_out_left);
        index--;
        if (index<0)
            index=images.length-1;
        ImageView imageView = (ImageView)viewSwitcher.getNextView();
        imageView.setImageResource(images[index]);
        viewSwitcher.showPrevious();
    }
    public void next(View view){
        viewSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
        viewSwitcher.setOutAnimation(this,android.R.anim.slide_out_right);
        index++;
        if (index>images.length-1)
            index=0;
        ImageView imageView = (ImageView)viewSwitcher.getNextView();
        imageView.setImageResource(imagext();

    }
}

当然,也可以自己定义动画:这里写图片描述

<translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="600"/>
<alpha android:fromAlpha="1"
        android:toAlpha="1"
        android:duration="600"/>

猜你喜欢

转载自blog.csdn.net/feiqinbushizheng/article/details/78868020