Android Gesture Recognition (click the double tap to lift the short press long Press to scroll)

Source: Internet
Author: User

For the touchscreen, its native message simply presses, lifts, and moves these kinds of things, we just need to simply overload Ontouch or set the touch listener Setontouchlistener can be processed. However, in order to improve the user experience of our app, sometimes we need to identify the user's gestures, and Android gives us the gesture recognition tool gesturedetector can be a big help.

    • Basis

Gesturedetector works by giving the message to Gesturedetector when we receive the user's touch message, and we get the gesturedetector-processed gesture by setting the listener.

Gesturedetector provides two listener interfaces, Ongesturelistener handles click class messages, and Ondoubletaplistener handles double-click class messages.

The Ongesturelistener interface has these several:

  1. Click to trigger the touch screen as soon as it is pressed
  2. Abstract Boolean Ondown (Motionevent e);
  3. Lift, trigger when your finger leaves the touch screen (long press, scroll, swipe, does not trigger this gesture)
  4. Abstract Boolean onsingletapup (Motionevent e);
  5. Short press, touch screen pressed after a moment to lift, will trigger this gesture, if the rapid lifting will not
  6. abstract void Onshowpress (Motionevent e);
  7. Press and hold, touch the screen after press neither lift nor move, after a period of time triggered
  8. abstract void Onlongpress (Motionevent e);
  9. Scroll, touch screen to move after pressing
  10. Abstract Boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey);
  11. Swipe, the touch screen presses and moves quickly and lifts, triggering a scrolling gesture, followed by a swipe gesture
  12. Abstract Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy);

The Ondoubletaplistener interface has these several:

    1. Double click, the finger on the touch screen quickly click on the second when the trigger
    2. Abstract Boolean Ondoubletap (Motionevent e);
    3. Double-click to press and lift each trigger once
    4. Abstract Boolean ondoubletapevent (Motionevent e);
    5. Click Confirm, which is quickly pressed and lifted, but not consecutively click Second
    6. Abstract Boolean onsingletapconfirmed (Motionevent e);


Sometimes we don't need to deal with all of the above gestures, for convenience, Android provides another class Simpleongesturelistener implements the above interface, We just need to inherit the Simpleongesturelistener and reload the gesture of interest.

    • Application

STEP 1: Create a Gesture Listener object

  1. Package noodies.blog.csdn.net;
  2. Import Android.content.Context;
  3. Import android.view.MotionEvent;
  4. Import Android.view.GestureDetector.SimpleOnGestureListener;
  5. Import Android.widget.Toast;
  6. public class Mygesturelistener extends Simpleongesturelistener {
  7. Private Context Mcontext;
  8. Mygesturelistener (Context context) {
  9. Mcontext = context;
  10. }
  11. @Override
  12. public boolean Ondown (Motionevent e) {
  13. Toast.maketext (Mcontext, "Down" + e.getaction (), Toast.length_short). Show ();
  14. return false;
  15. }
  16. @Override
  17. public void Onshowpress (Motionevent e) {
  18. Toast.maketext (Mcontext, "show" + e.getaction (), Toast.length_short). Show ();
  19. }
  20. @Override
  21. public boolean onsingletapup (Motionevent e) {
  22. Toast.maketext (Mcontext, "single Up" + E.getaction (), Toast.length_short). Show ();
  23. return false;
  24. }
  25. @Override
  26. public boolean onscroll (Motionevent E1, motionevent E2,
  27. Float Distancex, float distancey) {
  28. Toast.maketext (Mcontext, "SCROLL" + e2.getaction (), Toast.length_short). Show ();
  29. return false;
  30. }
  31. @Override
  32. public void Onlongpress (Motionevent e) {
  33. Toast.maketext (Mcontext, "LONG" + e.getaction (), Toast.length_short). Show ();
  34. }
  35. @Override
  36. public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
  37. Float velocityy) {
  38. Toast.maketext (Mcontext, "FLING" + e2.getaction (), Toast.length_short). Show ();
  39. return false;
  40. }
  41. @Override
  42. public boolean Ondoubletap (Motionevent e) {
  43. Toast.maketext (Mcontext, "DOUBLE" + e.getaction (), Toast.length_short). Show ();
  44. return false;
  45. }
  46. @Override
  47. public boolean ondoubletapevent (Motionevent e) {
  48. Toast.maketext (Mcontext, "DOUBLE EVENT" + e.getaction (), Toast.length_short). Show ();
  49. return false;
  50. }
  51. @Override
  52. public boolean onsingletapconfirmed (Motionevent e) {
  53. Toast.maketext (Mcontext, "single CONF" + E.getaction (), Toast.length_short). Show ();
  54. return false;
  55. }
  56. }

STEP 2: Set gesture recognition

We can set up gesture recognition in activity:

  1. Package noodies.blog.csdn.net;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.view.GestureDetector;
  5. Import android.view.MotionEvent;
  6. public class Gesturetestactivity extends Activity {
  7. Private Gesturedetector Mgesturedetector;
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. Super.oncreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. Mgesturedetector = new Gesturedetector (This, new Mygesturelistener (this));
  13. }
  14. @Override
  15. public boolean ontouchevent (Motionevent event) {
  16. Return Mgesturedetector.ontouchevent (event);
  17. }
  18. }

You can also set gesture recognition in a custom view:

  1. Package noodies.blog.csdn.net;
  2. Import Android.content.Context;
  3. Import Android.util.AttributeSet;
  4. Import Android.view.GestureDetector;
  5. Import android.view.MotionEvent;
  6. Import Android.view.View;
  7. public class MyView extends View {
  8. Private Gesturedetector Mgesturedetector;
  9. Public MyView (context context, AttributeSet Attrs) {
  10. Super (context, attrs);
  11. Mgesturedetector = new Gesturedetector (context, new Mygesturelistener (context));
  12. Setlongclickable (TRUE);
  13. This.setontouchlistener (New Ontouchlistener () {
  14. public boolean OnTouch (View V, motionevent event) {
  15. Return Mgesturedetector.ontouchevent (event);
  16. }
  17. });
  18. }
  19. }

    • Trap


For a custom view, using gesture recognition with two traps can be a waste of your time.

1:view must set Longclickable to true, otherwise gesture recognition will not work correctly, only return down, Show, long three gestures

2: Gesture recognition must be invoked in the view's Ontouchlistener, not overloaded ontouchevent like activity, or the same gesture recognition will not work correctly

    • Test results

  1. click: down 0, single up 1, single conf 0  
  2. short press: down 0, show 0, single up 1  
  3. long press: down 0, show 0, long 0  
  4. scroll: down 0,   (show 0),  scrool 2...  
  5. swipe: down  0,  (show 0),  scrool 2..., fling 1  

Android gesture recognition (click Double tap to lift short press long to scroll)

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.