<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; } });