Look directly at the code
1.mainactivity.java:
Package Org.yayun.demo;import Android.app.activity;import Android.os.bundle;import android.view.GestureDetector; Import Android.view.motionevent;import Android.view.viewgroup.layoutparams;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.ImageView; Import Android.widget.viewflipper;public class Mainactivity extends Activity Implementsandroid.view.GestureDetector.OnGestureListener {private int[] IMGs = {R.DRAWABLE.IMG1, r.drawable.img2, R.DRAWABLE.IMG3};p rivate gesturedetector gesturedetector;private viewflipper viewflipper;private Activity mActivity; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); mactivity = This;viewflipper = (viewflipper) Findviewbyid (r.id.viewflipper); gestureDetector = new Gesturedetector (this);//gesture listener for (int i = 0; i < imgs.length; i++) {//Add picture source ImageView IV = new ImageView (this); IV . Setimageresource (Imgs[i]); Iv.setscaleType (ImageView.ScaleType.FIT_XY); Viewflipper.addview (iv, New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent));} Viewflipper.setautostart (TRUE); Set your own active Play function (click on the event, before you actively play) Viewflipper.setflipinterval (3000);//Interval 3 seconds if (Viewflipper.isautostart () &&! Viewflipper.isflipping ()) {viewflipper.startflipping ();}} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubviewflipper.stopflipping (); After clicking the event. Stop yourself from actively playing Viewflipper.setautostart (false); return Gesturedetector.ontouchevent (event); Manual Gesture Event}public boolean ondown (motionevent arg0) {//TODO auto-generated method Stubreturn false;} public boolean onfling (Motionevent E1, motionevent E2, float arg2,float arg3) {if (E2.getx ()-e1.getx () > 120) {//From left Swipe right (left-in-right out) Animation Rinanim = Animationutils.loadanimation (mactivity,r.layout.push_right_in); Slide right to the left side into the gradient effect (Alpha 0.1-1.0) Animation Routanim = Animationutils.loadanimation (mactivity,r.layout.push_right_ Out); Swipe right to slide out the gradient effect (alpha 1.0-0.1) viewflipper.setinanimation (Rinanim); viewflipper.setoutanimation (Routanim); Viewflipper.showprevious () ; return true;} else if (E2.getx ()-E1.getx () <-120) {//swipe from right to left (right-hand out) Animation Linanim = Animationutils.loadanimation (mactivity,r.la YOUT.PUSH_LEFT_IN); Slide left side into the gradient effect (alpha 0.1, 1.0) Animation Loutanim = Animationutils.loadanimation (Mactivity,r.layout.push_left_ Out); Swipe left to slide out the gradient effect (Alpha 1.0-0.1) viewflipper.setinanimation (Linanim); viewflipper.setoutanimation (Loutanim); Viewflipper.shownext (); return true;} return true;} public void onlongpress (Motionevent arg0) {//TODO auto-generated method Stub}public boolean onscroll (motionevent arg0, Mo Tionevent arg1, float arg2,float arg3) {//TODO auto-generated method Stubreturn false;} public void onshowpress (Motionevent arg0) {//TODO auto-generated method Stub}public boolean onsingletapup (motionevent ar G0) {//TODO auto-generated method Stubreturn false;}}
2.active_main.xml:
<?XML version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <viewflipper android:id= "@+id/viewflipper" android:layout_width= "fill _parent " android:layout_height=" fill_parent "/> </LinearLayout>
See the following four animated files, very easy.
1.push_left_in.xml
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android: duration= "Android:fromxdelta=" 100%p " android:toxdelta=" 0 "/> <!--shift- -- < Alpha android:duration= "android:fromalpha=" 0.1 " android:toalpha=" 1.0 "/> <!--gradients
2.push_left_out.xml
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/ Android "> <translate android:duration=" android:fromxdelta= " 0" Android:toxdelta = " -100%p"/> <alpha android:duration= "$" android:fromalpha= "1.0" android:toalpha= " 0.1 "/> </set>
3.push_right_in.xml
<?XML version= "1.0" encoding= "Utf-8"?
> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android: duration= "Android:fromxdelta=" -100%p " android:toxdelta=" 0 "/> <alpha android: duration= "android:fromalpha=" 0.1 " android:toalpha=" 1.0 "/> </set>
4.push_right_out.xml
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android: duration= "Android:fromxdelta=" 0 " android:toxdelta=" 100%p "/> <alpha android: duration= "android:fromalpha=" 1.0 " android:toalpha=" 0.1 "/> </set>
Perform the actual proportions as follows:
Ask for attention, thank you
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Android Really simple tutorial-12th gun (Viewflipper realize sliding effect)