Android Pictures Carousel and swipe

Source: Internet
Author: User

Using Viewfipper to achieve

Mainactivity

1  PackageViewflipper.example.administrator.viewfipper;2 3 Importandroid.app.Activity;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 Importandroid.view.MotionEvent;7 ImportAndroid.widget.ImageView;8 ImportAndroid.widget.ViewFlipper;9 Ten  Public classMainactivityextendsActivity { One     PrivateViewflipper Viewflipper; A     int[] res={R.DRAWABLE.PIC1,R.DRAWABLE.PIC2,R.DRAWABLE.PIC3,R.DRAWABLE.PIC4}; -     Private floatX; - @Override the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); -Viewflipper=(Viewflipper) Findviewbyid (r.id.flipper); +          for(inti=0;i<res.length;i++){ - Viewflipper.addview (GetImage (res[i)); +         } A //viewflipper.setinanimation (this,r.anim.left_in);/* Set animation to go in from the left */ at //viewflipper.setoutanimation (this,r.anim.left_out); - //Viewflipper.setflipinterval (3000);/* Set Carousel time */ - //viewflipper.startflipping (); -  -     } -  in      PublicImageView GetImage (intRes) { -ImageView imageview=NewImageView ( This); toImageview.setbackgroundresource (RES);/*Set Picture fullscreen*/ + //Imageview.setimageresource (res); -         returnImageView; the     } *  $ @OverridePanax Notoginseng      Public Booleanontouchevent (motionevent motionevent) { -  the         Switch(Motionevent.getaction ()) { + //Falling Fingers A              Casemotionevent.action_down:{ theX =motionevent.getx (); +                  Break;} - //Finger Swipe $              CaseMOTIONEVENT.ACTION_UP: {/*Swipe left, swipe right if it's written in Action_move, the picture will overlap because it will be met multiple times*/ $ //Swipe Right -                 if(Motionevent.getx ()-X > 100) { -Viewflipper.setinanimation ( This, r.anim.left_in); theViewflipper.setoutanimation ( This, r.anim.left_out); - viewflipper.showprevious ();Wuyi                 } the                 if(X-motionevent.getx () > 100) { -Viewflipper.setinanimation ( This, r.anim.right_in); WuViewflipper.setoutanimation ( This, r.anim.right_out); - Viewflipper.shownext (); About                 } $                  Break; -             } - //Finger away -              CaseMotionevent.action_move:/*don't write Action_hover_move .*/ A                  Break; +         } the  -         return Super. Ontouchevent (motionevent); $     } the}

Main.xml

1 <?xml version= "1.0" encoding= "Utf-8"?>2 <linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android "3     android:orientation=" vertical "android:layout_width=" Match_ Parent "4     android:layout_height=" Match_parent ">5 <viewflipper6     android:layout_width= "Match_parent"7     android:layout_height= "match_parent"8     android:id= "@+id/flipper" ></ViewFlipper>9 </LinearLayout>

Anim in Left_in.xml

1<?xml version= "1.0" encoding= "Utf-8"?>2<set xmlns:android= "Http://schemas.android.com/apk/res/android" >3 4<Translate5Android:duration= "2000"6Android:fromxdelta= " -100%p"7Android:toxdelta= "0"/>8<Alpha9Android:fromalpha= "0.5"TenAndroid:toalpha= "1" OneAndroid:duration= "2000" A/> -  -</set>

Right_in.xml

  1  <?xml version=" 1.0 "encoding=" Utf-8 "?>  <set xmlns:android=" Http://schemas.android.com/apk/res/android ">< Span style= "color: #008080;" > 3   4  <translate   5  android:duration= " 6  android:fromxdelta=" 100%p " 7  android:toxdelta= "0"/> 8   9 <alpha  10  android:duration= "200 0 "11  android:fromalpha=" 0.5 "12  A Ndroid:toalpha= "1"/>13  

Left_out.xml

  1  <?xml version=" 1.0 "encoding=" Utf-8 "?>  <set xmlns:android=" Http://schemas.android.com/apk/res/android ">< Span style= "color: #008080;" > 3   4  <translate   5  android:duration= " 6  android:fromxdelta=" 0 " 7  android:toxdelta= "100%p"/> 8   9< /span> <alpha  11  android:fromalpha=" 0.5 "12  Android:toalpha= "1"/>13  

Right_out.xml

  1  <?xml version=" 1.0 "encoding=" Utf-8 "?>  <set xmlns:android=" Http://schemas.android.com/apk/res/android ">< Span style= "color: #008080;" > 3   4  <translate   5  android:duration= " 6  android:fromxdelta=" 0 " 7  android:toxdelta= " -100%p"/> 8   9  <alpha  10  android:duration         = "11  android:fromalpha=" 0.5 "12  Android:toalpha= "1"/>13  

Android Pictures Carousel and swipe

Related Article

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.