Android ImageSwitch(翻页图片浏览)

<ImageSwitcher
    android:id="@+id/switcher"
    android:layout_width="match_parent"
    android:layout_height="408dp"
    android:layout_marginBottom="103dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"></ImageSwitcher>
private  int[] arrayPicture=new int []{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4};
    private ImageSwitcher imsw;
    private int index;
    private float down;
    private float up;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_act);
        GridView gridView=findViewById(R.id.grideview);
        setTitle("滚动选择显示图片");
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
         imsw=findViewById(R.id.switcher);
        imsw.setOutAnimation(AnimationUtils.loadAnimation(imageACT.this,android.R.anim.fade_in));
        imsw.setOutAnimation(AnimationUtils.loadAnimation(imageACT.this,android.R.anim.fade_out));
        imsw.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView=new ImageView(imageACT.this);
                imageView.setImageResource(R.drawable.ic_launcher_background);
                return imageView;
            }
        });
//        imsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ImageSwitcher)v).setImageResource(R.drawable.ic_launcher_foreground);
            }
        });
        imsw.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    down = event.getX();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    up = event.getX();
                    if (up - down > 100) {
                        index = index == 0 ? arrayPicture.length - 1 : index - 1;
                        //imsw.setInAnimation(AnimationUtils.loadAnimation(imageACT.this,R.));
                        imsw.setImageResource(arrayPicture[index]);
                    }
                    else if (down - up > 100) {
                    index = index == arrayPicture.length - 1 ? 0 : index + 1;
                    //imsw.setInAnimation(AnimationUtils.loadAnimation(imageACT.this,R.));
                    imsw.setImageResource(arrayPicture[index]);
                }
                return true;
            }

                return false;
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值