ViewFlipper implements page switching of ViewPager and page switching of viewpager
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ViewFlipper></LinearLayout>
MainActivity
Package com. example. viewflipper; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. widget. imageView; import android. widget. viewFlipper; public class MainActivity extends Activity {private ViewFlipper flipper; private int [] resId = {R. drawable. pic1, R. drawable. pic2, R. drawable. pic3, R. drawable. pic4}; private float startX; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); flipper = (ViewFlipper) findViewById (R. id. flipper); // Add the child viewfor (int I = 0; I <resId. length; I ++) {flipper. addView (getImageView (resId [I]);} // enter the animation-cancel the automatic playback function // flipper. setInAnimation (this, R. anim. left_in); // flipper. setOutAnimation (this, R. anim. left_out); // flipper. setFlipInterval (3000); // flipper. startFlipping () ;}// obtain the image private ImageView getImageView (int resId) {ImageView imageView = new ImageView (this); // overlay the screen imageView. setBackgroundResource (resId); return imageView;}/*** process gesture events */@ Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // obtain the X axis coordinate of the starting point startX = event. getX (); break; case MotionEvent. ACTION_MOVE: break; case MotionEvent. ACTION_UP: // slide to the right if (event. getX () & gt; startX + 100) {flipper. setInAnimation (this, R. anim. left_in); flipper. setOutAnimation (this, R. anim. rightf_out); // display the previous flipper page. showPrevious ();} // move if (event. getX () + 100 <startX) {flipper. setInAnimation (this, R. anim. right_in); flipper. setOutAnimation (this, R. anim. left_out); // display the previous flipper page. showNext ();} break; default: break;} return super. onTouchEvent (event );}}
Left_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="-100%p" android:toXDelta="0" /></set>
Left_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="-100%p" /></set>
Right_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="100%p" android:toXDelta="0" /></set>
Rightf_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="100%p" /></set>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.