Thank you !!!
Reprinted Please note: http://blog.csdn.net/richway2010/archive/2011/06/29/6574987.aspx
Bloggers: All bloggers, netizens, and everyone on the Internet! Welcome to this blog. You are welcome to have more exchanges, give more comments, learn from each other, and make mutual progress. Our slogan is: study hard and make progress every day .]
Go directly to the topic to see the effect:
Touch the screen from right to left:
Implementation: Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout Android: layout_height = "fill_parent" <br/> Android: layout_width = "fill_parent" <br/> Android: Orientation = "vertical" xmlns: android = "http://schemas.android.com/apk/res/android" <br/> <viewflipper Android: Id = "@ + ID/Flipper" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> <br/> <include Android: id = "@ + ID/firstlayout" layout = "@ layout/left"/> <br/> <include Android: id = "@ + ID/secondlayout" layout = "@ layout/center"/> <br/> <include Android: id = "@ + ID/thirdlayout" layout = "@ layout/right"/> <br/> </viewflipper> <br/> </linearlayout>
Center. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout Android: gravity = "center_vertical" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_width = "fill_parent" xmlns: android = "http://schemas.android.com/apk/res/android"> <br/> <imageview Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: src = "@ drawable/A3"> <br/> </imageview> </linearlayout>
Left. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout Android: gravity = "center_vertical" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent" <br/> xmlns: android = "http://schemas.android.com/apk/res/android"> <br/> <imageview Android: layout_height = "wrap_content" <br/> Android: layout_width = "wrap_content" Android: src = "@ drawable/A2"> <br/> </imageview> <br/> </linearlayout>
Right. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout Android: gravity = "center_vertical" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent" <br/> xmlns: android = "http://schemas.android.com/apk/res/android"> <br/> <imageview Android: layout_height = "wrap_content" <br/> Android: layout_width = "wrap_content" Android: src = "@ drawable/A1"> </imageview> <br/> </linearlayout>
Main configuration file: androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "org. flipper "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". viewflipperdemoactivity "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> </Application> <br/> </manifest>
Main class: viewflipperdemoactivity. Java
Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. gesturedetector; <br/> Import android. view. motionevent; <br/> Import android. view. view; <br/> Import android. view. gesturedetector. ongesturelistener; <br/> Import android. view. view. ontouchlistener; <br/> Import android. view. animation. accelerateinterpolator; <br/> Import android. view. animation. animation; <br/> Import android. view. animation. translateanimation; <br/> Import android. widget. viewflipper; <br/> public class extends activity implements ongesturelistener, ontouchlistener {<br/> private viewflipper mflipper; <br/> gesturedetector listener; <br/> private int mcurrentlayoutstate; <br/> Private Static final int fling_min_distance = 120; <br/> Private Static final int fling_min_velocity = 240; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> mflipper = (viewflipper) findviewbyid (R. id. flipper); <br/> // register a class for Gesture Recognition <br/> mgesturedetector = new gesturedetector (this ); <br/> // set a listener for mflipper <br/> mflipper. setontouchlistener (this); <br/> mcurrentlayoutstate = 0; <br/> // you can hold down viewflipper for a long time to recognize the drag and other gestures. <br/> mflipper. setlongclickable (true ); </P> <p>/** <br/> * define the animation effect from the right <br/> * @ return <br/> */<br/> protected animation infromrightanimation () {<br/> animation infromright = new translateanimation (<br/> animation. relative_to_parent, + 1.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f); <br/> infromright. setduration( 500); <br/> infromright. setinterpolator (New accelerateinterpolator (); <br/> return infromright; <br/>}</P> <p>/** <br/> * define the animation effect to exit from the left <br/> * @ return <br/> */ <br/> protected animation outtoleftanimation () {<br/> animation outtoleft = new translateanimation (<br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent,-1.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f); <br/> outtoleft. setduration (1, 500); <br/> outtoleft. setinterpolator (New accelerateinterpolator (); <br/> return outtoleft; <br/>}</P> <p>/** <br/> * define the animation effect from the left <br/> * @ return <br/> */ <br/> protected animation infromleftanimation () {<br/> animation infromleft = new translateanimation (<br/> animation. relative_to_parent,-1.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f); <br/> infromleft. setduration( 500); <br/> infromleft. setinterpolator (New accelerateinterpolator (); <br/> return infromleft; <br/>}</P> <p>/** <br/> * define the animation effect when exiting from the right <br/> * @ return <br/> * /<br/> protected animation outtorightanimation () {<br/> animation outtoright = new translateanimation (<br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, + 1.0f, <br/> animation. relative_to_parent, 0.0f, <br/> animation. relative_to_parent, 0.0f); <br/> outtoright. setduration (1, 500); <br/> outtoright. setinterpolator (New accelerateinterpolator (); <br/> return outtoright; <br/>}</P> <p> Public Boolean ondown (motionevent E) {<br/> // todo auto-generated method stub <br/> return false; <br/>}</P> <p>/* <br/> * this event is triggered when the user presses the touch screen and moves quickly. <br/> * E1: 1st action_down motionevents <br/> * E2: Last action_move motionevent <br/> * velocityx: moving speed on the X axis, pixel/second <br/> * velocityy: moving speed on the Y axis, pixel/second <br/> * trigger condition: <br/> * coordinate displacement of the X axis is greater than fling_min_distance, and the moving speed is greater than fling_min_velocity pixels/second <br/> */<br/> Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, <br/> float velocityy) {<br/> If (e1.getx ()-e2.getx ()> fling_min_distance <br/> & math. ABS (velocityx)> fling_min_velocity) {<br/> // set the animation used when the view enters the screen while sliding on the left <br/> mflipper. setinanimation (infromrightanimation (); <br/> // sets the animation used when the view exits the screen. <br/> mflipper. setoutanimation (outtoleftanimation (); <br/> mflipper. shownext (); <br/>}else if (e2.getx ()-e1.getx ()> fling_min_distance <br/> & math. ABS (velocityx)> fling_min_velocity) {<br/> // when the image slides to the right <br/> mflipper. setinanimation (infromleftanimation (); <br/> mflipper. setoutanimation (outtorightanimation (); <br/> mflipper. showprevious (); <br/>}< br/> return false; <br/>}< br/> Public Boolean ontouch (view V, motionevent event) {<br/> // send the touch screen event to the gesture recognition class for processing <br/> return mgesturedetector. ontouchevent (event); <br/>}< br/>
Thank you for your help.