There are two ways to implement android rotograph, viewPager and viewFlipper. A while ago, I needed to use viewPager in my project, but it was too much code, so I gave it up. I used viewFlipper before, and I thought it was easier. The animation is not as good as viewPager, but the overall code is minimal. Ok, let’s go to the code.

The first step is to get the ViewFlipper and add an imageView to the ViewFlipper

vf_image = (ViewFlipper) findViewById(R.id.vf_image);

imageList.add(R.drawable.show1);

imageList.add(R.drawable.show2);

imageList.add(R.drawable.show3);

for (int i = 0; i <imageList.size(); i++) {

ImageView imageView =new ImageView(getApplicationContext());

imageView.setBackgroundResource(imageList.get(i));

vf_image.addView(imageView);

}

Set the auto-play and interval to the ViewFlipper and start it. The auto-scroll image is done, isn’t it

vf_image.setAutoStart(true); // Set auto play function (click event, auto play before)

vf_image.setFlipInterval(3000);

if (vf_image.isAutoStart() && ! vf_image.isFlipping()) {

vf_image.startFlipping();

}

The OnGestureListener listener is used to listen on the whole screen of the phone. Swipe the screen blank and the picture will change

vf_image.setOnTouchListener(new OnTouchListener() {

@SuppressLint(“NewApi”)

@Override

publicboolean onTouch(View v, MotionEvent event) {

//TODO Auto-generated method stub

if (event.getAction() == MotionEvent.ACTION_DOWN) {

//TODO presses when touched

x1 = event.getX();

}

if (event.getAction() == MotionEvent.ACTION_UP) {

//TODO lifts when touched

x2 = event.getX();

if (x1 -x2 < -300) {

x1 = 0;

x2 = 0;

vf_image.showPrevious();

}else if (x1 -x2 > 300) {

x1 = 0;

x2 = 0;

vf_image.showNext();

}

}

returntrue;

}

});

So far, gesture sliding and automatic sliding have been completed, only less than 50 lines of code to get it done, in addition, I did not add animation and rotation map that little dot, if you need to add it, the content is relatively basic, do not like spray