图像切换器ImageSwitcher extends ViewSwitcher

图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的“Windows照片查看器”中的上一张、下一张切换图片的功能。在使用ImageSwitcher时,必须实现ViewSwitcher.ViewFactory接口,并通过makeView()方法来创建用于显示图片的ImageView。makeView()方法将返回一个显示图片的ImageView。在使用图像切换器时,还有一个方法非常重要,那就是setImageResource方法,该方法用于指定要在ImageSwitcher中显示的图片资源。

public class MainActivity extends AppCompatActivity {

    private int[] arrayPicture=new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,
            R.drawable.img04,R.drawable.img05,R.drawable.img06,R.drawable.img07,
            R.drawable.img08,R.drawable.img09};
    private ImageSwitcher mImageSwitcher;
    private int index;//图片索引
    private float touchDownX;//手指按下时的横坐标
    private float touchUpX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏

        mImageSwitcher = findViewById(R.id.imageSwitcher);
        mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(MainActivity.this);
                //设置保持纵横比居中缩放图像
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setImageResource(arrayPicture[index]);
                return imageView;
            }
        });

        mImageSwitcher.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //获取手指滑动时的横坐标
                if (event.getAction()==MotionEvent.ACTION_DOWN){
                    touchDownX=event.getX();
                    return true;
                }else if (event.getAction()==MotionEvent.ACTION_UP){
                    touchUpX=event.getX();
                    //判断是否从左向右滑动,>100是为了防止误滑
                    if (touchUpX-touchDownX>100){
                        index=(index==0?arrayPicture.length-1:index-1);
                        //设置左边进入的动画
                        mImageSwitcher.setInAnimation(AnimationUtils.
                                loadAnimation(MainActivity.this,R.anim.slide_in_left));
                        //从右边出的动画
                        mImageSwitcher.setOutAnimation(AnimationUtils.
                                loadAnimation(MainActivity.this,R.anim.slide_out_right));
                        mImageSwitcher.setImageResource(arrayPicture[index]);
                    }else if (touchDownX-touchUpX>100){
                        index=(index==arrayPicture.length-1?0:index+1);
                        //设置左边进入的动画
                        mImageSwitcher.setInAnimation(AnimationUtils.
                                loadAnimation(MainActivity.this,R.anim.slide_in_right));
                        //从右边出的动画
                        mImageSwitcher.setOutAnimation(AnimationUtils.
                                loadAnimation(MainActivity.this,R.anim.slide_out_left));
                        mImageSwitcher.setImageResource(arrayPicture[index]);
                    }
                    return true;
                }
                return false;
            }
        });
    }
}



    

你可能感兴趣的:(图像切换器ImageSwitcher extends ViewSwitcher)