Http://www.javaeye.com/topic/369122
The iPhone interface is unlocked with fingers. How can this gesture be implemented in Android?
The android SDK provides a listener class to detect different gestures:
Simpleongesturelistener. You only need to implement the gestures you care about.
In Android, swipe is called fling pai_^.
First, create your own gesture detector class: JavaCode
- ClassMygesturedetectorExtendsSimpleongesturelistener {
- @ Override
- Public BooleanOnfling (motionevent E1, motionevent E2,FloatVelocityx,FloatVelocityy ){
- }
Class mygesturedetector extends simpleongesturelistener {@ overridepublic Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){}
A stroke gesture has several features. For example, if it is almost a straight line on X or Y axis, the deviation of the midway path cannot be too large, and it requires a certain speed, so we define a few quantitative values: Java code
- private static final int swipe_min_distance = 120 ;
- private static final int swipe_max_off_path = 250 ;
- private static final int swipe_threshold_velocity = 200 ;
Private Static final int swipe_min_distance = 120; Private Static final int swipe_max_off_path = 250; Private Static final int swipe_threshold_velocity = 200;
Then, in the onfling method, determine if it is a reasonable swipe action: Java code
-
- If(E1.getx ()-e2.getx ()> swipe_min_distance & math. Abs (velocityx)> swipe_threshold_velocity ){
-
- Viewflipper. setinanimation (slideleftin );
-
- Viewflipper. setoutanimation (slideleftout );
-
- Viewflipper. shownext ();
- }Else If(E2.getx ()-e1.getx ()> swipe_min_distance & math. Abs (velocityx)> swipe_threshold_velocity ){
-
- Viewflipper. setinanimation (sliderightin );
-
- Viewflipper. setoutanimation (sliderightout );
-
- Viewflipper. showprevious ();
-
- }
If (e1.getx ()-e2.getx ()> swipe_min_distance & math. ABS (velocityx)> swipe_threshold_velocity) {viewflipper. setinanimation (slideleftin); viewflipper. setoutanimation (slideleftout); viewflipper. shownext ();} else if (e2.getx ()-e1.getx ()> swipe_min_distance & math. ABS (velocityx)> swipe_threshold_velocity) {viewflipper. setinanimation (sliderightin); viewflipper. setoutanimation (sliderightout); viewflipper. showprevious ();}
Viewflipper is a container containing multiple views. You can easily call Prev/next view and add the animation to achieve some good results: Java code.
- viewflipper = (viewflipper) findviewbyid (R. Id. Flipper);
- slideleftin = animationutils. loadanimation ( This , R. anim. slide_left_in);
- slideleftout = animationutils. loadanimation ( This , R. anim. slide_left_out);
- sliderightin = animationutils. loadanimation ( This , R. anim. slide_right_in);
- sliderightout = animationutils. loadanimation ( This , R. anim. slide_right_out);
Viewflipper = (viewflipper) findviewbyid (R. id. flipper); slideleftin = animationutils. loadanimation (this, R. anim. slide_left_in); slideleftout = animationutils. loadanimation (this, R. anim. slide_left_out); sliderightin = animationutils. loadanimation (this, R. anim. slide_right_in); sliderightout = animationutils. loadanimation (this, R. anim. slide_right_out );
Custom Animation allows you to view specific XML, such as an animation on the left: XML Code
-
- <Set Xmlns: Android=Http://schemas.android.com/apk/res/android">
- <Translate Android: fromxdelta="100% P" Android: toxdelta="0" Android: Duration="800"/>
-
- </Set>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android"> <translate Android: fromxdelta = "100% P" Android: toxdelta = "0" Android: Duration = "800"/> </set>
Of course, do not forget to use the override ontouch method in your activity to obtain the gesture action: Java code.
- @ Override
- Public BooleanOntouchevent (motionevent event ){
- If(Gesturedetector. ontouchevent (event ))
- Return True;
- Else
- Return False;
- }
@ Overridepublic Boolean ontouchevent (motionevent event) {If (gesturedetector. ontouchevent (event) return true; elsereturn false ;}