Android slide gesture Detection Method

Source: Internet
Author: User

The Android mobile phone operating system supports various sliding gestures. Here we will introduce how these gestures can be detected on the Android platform. Next let's take a look at the specific recognition methods of Android sliding gestures.

  • Android Activity jump related operation tips
  • Android sdcard Creation
  • Details on how to view Android system information
  • Android database code explanation
  • Overview

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.

First, create your own gesture detector class:

 
 
  1. class MyGestureDetector extends SimpleOnGestureListener {   
  2. @Override   
  3. public boolean onFling(MotionEvent e1, MotionEvent e2, 
    float velocityX, float velocityY) {   

An Android slide gesture has several features. For example, it is almost a straight line on the x or Y axis. The deviation between the midway path cannot be too large, and it requires a certain speed. Therefore, we define several quantiles:

 
 
  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;  

Then, in the onFling method, determine whether it is a reasonable swipe action:

 
 
  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. }   
  10. if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && 
    Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  11. viewFlipper.setInAnimation(slideLeftIn);  
  12. viewFlipper.setOutAnimation(slideLeftOut);  
  13. viewFlipper.showNext();  
  14. } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && 
    Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  15. viewFlipper.setInAnimation(slideRightIn);  
  16. viewFlipper.setOutAnimation(slideRightOut);  
  17. 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:

 
 
  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);  

Custom animation allows you to view specific XML, such as an animation coming in from the left:

 
 
  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> 

Of course, do not forget to use the override onTouch method in your Activity to obtain the gesture action:

 
 
  1. @Override   
  2. public boolean onTouchEvent(MotionEvent event) {   
  3. if (gestureDetector.onTouchEvent(event))   
  4. return true;   
  5. else   
  6. return false;   

Android sliding gesture recognition methods are described here.

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.