Android Combat Simple tutorial-12th gun (Viewflipper to achieve the slideshow effect)

Source: Internet
Author: User

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 AutoPlay function (click event, pre-AutoPlay) Viewflipper.setflipinterval (3000);//3 Seconds interval (Viewflipper.isautostart () &&! Viewflipper.isflipping ()) {viewflipper.startflipping ();}} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubviewflipper.stopflipping (); After clicking the event, stop AutoPlay Viewflipper.setautostart (false); return Gesturedetector.ontouchevent (event); Register 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); Swipe right to the left to enter the gradient effect (Alpha 0.1-1.0) Animation Routanim = Animationutils.loadanimation (mactivity,r.layout.push_right_ Out); Swipe right to the right to slide 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); Swipe left to enter the gradient effect (Alpha 0.1-1.0) Animation Loutanim = Animationutils.loadanimation (Mactivity,r.layout.push_left_ Out); Swipe left to slide 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>  


Here's a look at four animated files, very simple.

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"/>  <!--displacement--        <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>

Run the following example:

Ask for attention, thank you

Android Combat Simple tutorial-12th gun (Viewflipper to achieve the slideshow effect)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.