Implement the swipe gesture function and page drag animation in Android

Source: Internet
Author: User

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

    1. ClassMygesturedetectorExtendsSimpleongesturelistener {
    2. @ Override
    3. Public BooleanOnfling (motionevent E1, motionevent E2,FloatVelocityx,FloatVelocityy ){
    4. }
 
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

    1. private static final int swipe_min_distance = 120 ;
    2. private static final int swipe_max_off_path = 250 ;
    3. 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

  1. If(E1.getx ()-e2.getx ()> swipe_min_distance & math. Abs (velocityx)> swipe_threshold_velocity ){
  2. Viewflipper. setinanimation (slideleftin );
  3. Viewflipper. setoutanimation (slideleftout );
  4. Viewflipper. shownext ();
  5. }Else If(E2.getx ()-e1.getx ()> swipe_min_distance & math. Abs (velocityx)> swipe_threshold_velocity ){
  6. Viewflipper. setinanimation (sliderightin );
  7. Viewflipper. setoutanimation (sliderightout );
  8. Viewflipper. showprevious ();
  9. }
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.

    1. viewflipper = (viewflipper) findviewbyid (R. Id. Flipper);
    2. slideleftin = animationutils. loadanimation ( This , R. anim. slide_left_in);
    3. slideleftout = animationutils. loadanimation ( This , R. anim. slide_left_out);
    4. sliderightin = animationutils. loadanimation ( This , R. anim. slide_right_in);
    5. 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

  1. <Set Xmlns: Android=Http://schemas.android.com/apk/res/android">
  2. <Translate Android: fromxdelta="100% P" Android: toxdelta="0" Android: Duration="800"/>
  3. </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.

    1. @ Override
    2. Public BooleanOntouchevent (motionevent event ){
    3. If(Gesturedetector. ontouchevent (event ))
    4. Return True;
    5. Else
    6. Return False;
    7. }
@ Overridepublic Boolean ontouchevent (motionevent event) {If (gesturedetector. ontouchevent (event) return true; elsereturn false ;}

 

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.