In fact, in android, the Gallery view Gallery and ImageSwitcher components are used to introduce the use of ImageSwitcher. Today, a custom ImageSwitcher is encapsulated, it has the gesture slide effect and animation effect, and provides an interface for automatic switching. You can use this function directly in the future. The Code is as follows:
MyImageSwitcher:
Package com. home. testimageswitcher; import android. content. context; import android. OS. handler; import android. OS. message; import android. view. gestureDetector; import android. view. motionEvent; import android. view. view; import android. view. gestureDetector. onGestureListener; import android. widget. imageSwitcher; import android. widget. imageView; import android. widget. toast; public class MyImageSwitcher extends Ima GeSwitcher {private Thread t; // automatically switch the image Thread private GestureDetector detector; private Context context; private int index; // private int [] imageIds; public boolean hasStarted = false; // whether the automatic switch API private boolean isSwitching = false is enabled; // whether the image public boolean isDown = false is automatically switched; // whether the image is in the public boolean isFling = false status; // whether sliding private int ction = 1; // direction: 1: Sliding to the left;-1: Sliding to the right: public Handler handler = new Ha Ndler () {public void handleMessage (Message msg) {if (msg. what = 1) {setImage (direction) ;}}; public MyImageSwitcher (Context context, int [] imageIds) {super (context); init (context, imageIds );} class MyOnGestureListener implements OnGestureListener {@ Overridepublic boolean onDown (MotionEvent e) {isDown = true; return false ;}@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, Float velocityY) {isFling = true; isSwitching = false; // stop switching during the Sliding Process float instance = e1.getX ()-e2.getX (); if (instance> 10) {direction = 1; index ++; if (index> imageIds. length-1) {index = 0;} setImage (direction);} else if (instance <-10) {direction =-1; index --; if (index <0) {index = imageIds. length-1 ;}setimage (direction) ;}if (hasStarted) {startSwitching () ;}return false ;}@ Overridepublic void on LongPress (MotionEvent e) {}@ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false ;}@ Overridepublic void onShowPress (MotionEvent e) {}@ Overridepublic boolean onSingleTapUp (MotionEvent e) {return false ;}@overridepublic boolean onTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_UP) {if (isDown &&! IsFling) {Toast. makeText (context, "Click image", Toast. LENGTH_SHORT ). show () ;}isdown = false; isFling = false;} return super. onTouchEvent (event);}/*** initialize ** @ param context */private void init (Context context, int [] imageIds) {if (imageIds = null | imageIds. length <= 0) {return;} this. context = context; this. imageIds = imageIds; if (imageIds. length> 1) {this. detector = new GestureDetector (context, new MyOn GestureListener (); this. setOnTouchListener (new MyOnTouchListener (); this. setLongClickable (true);} this. setFactory (new MyFactory (); this. setImageResource (imageIds [index]);} class MyOnTouchListener implements OnTouchListener {@ Overridepublic boolean onTouch (View v, MotionEvent event) {return detector. onTouchEvent (event);} class MyFactory implements ViewFactory {@ Overridepublic View makeView () {ImageVi Ew imageView = new ImageView (context); imageView. setBackgroundColor (0xFF000000); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); imageView. setLayoutParams (new ImageSwitcher. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); return imageView;}/*** start thread switching image */public void startSwitching () {if (imageIds = null | imageIds. length <= 1) {return;} isSwitching = true; if (t = null |! T. isAlive () {t = new Thread () {public void run () {while (isSwitching) {try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} hasStarted = true; if (direction = 1) {index ++;} else {index --;} if (index> imageIds. length-1) {index = 0;} if (index <0) {index = imageIds. length-1;} Message msg = new Message (); msg. what = 1; handler. sendMessage (msg) ;};}; t. start () ;}}/*** set the image and animation effect in the direction ** @ param direction */private void setImage (int direction) {if (direction = 1) {setInAnimation (context, R. anim. push_left_in); setOutAnimation (context, R. anim. push_left_out); setImageResource (imageIds [index]);} else {setInAnimation (context, R. anim. push_right_in); setOutAnimation (context, R. anim. push_right_out); setImageResource (imageIds [index]) ;}}
Call Activity:
Package com. home. testimageswitcher; import android. app. activity; import android. OS. bundle; public class MainActivity extends Activity {private int [] imageIds = {R. drawable. test1, R. drawable. test2, R. drawable. test3, R. drawable. test4, R. drawable. test5}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); MyImageSwitcher switcher = new MyImageSwitcher (this, imageIds); setContentView (switcher); switcher. startSwitching (); // enable automatic switching }}
Animation series:
Push_left_in.xml:
Push_left_out.xml:
Push_right_in.xml:
Push_right_out.xml: