[Excerpt] Android gesture Detector

Source: Internet
Author: User

Thanks to the many pioneers of Android Chinese developers, the main content of this article comes from the summary, a small part of which is my own experience. ReferencesArticleIs:

 

Http://www.williamhua.com/2009/04/23/android-touchscreen-gesture-recogniton/

Http://goro.iteye.com/blog/402163

 

Unlike the traditional click touch screen, Android touch screen has some gesture gestures, such as fling and scroll. These gesture greatly improves user experience. In Android, gesture recognition (detector) is implemented through the gesturedetector. ongesturelistener interface.

First, the Android event processing mechanism is implemented based on listener. For example, a touch screen event is implemented through ontouchlistener;

Secondly, all view subclasses can add listener for a certain type of events by using methods such as setontouchlistener () and setonkeylistener;

Third, listener is generally provided in interface mode, which contains one or more abstract methods. We need to implement these methods to complete ontouch (), onkey (), and other operations. In this way,ProgramYou can use the callback function to give an appropriate response when a specific event is sent to the view by dispatch.

 

1. Touch Screen click example

Java Code
  1. Public class mygesture extends activity implements ontouchlistener {
  2. Public void oncreate (bundle savedinstancestate ){
  3. Super. oncreate (savedinstancestate );
  4. Setcontentview (R. layout. Main );
  5. Textview TV = (textview) findviewbyid (R. Id. TV );
  6. TV. setontouchlistener (This );
  7. }
  8. Public Boolean ontouch (view V, motionevent event ){
  9. Toast. maketext (This, "Touch touch", Toast. length_short). Show ();
  10. Return false;
  11. }
  12. }

We can use the getaction () method of motionevent to obtain the touch event type, including action_down (by pressing the touch screen), action_move (by pressing the touch screen), action_up (by releasing the touch screen) and action_cancel (not directly triggered by the user ). After obtaining coordinates using getrawx (), getrawy (), getx (), getx (), and Gety () methods based on different user operations, we can implement operations such as dragging a button, drag a scroll bar.

 

2. Back to today's focus, how can we identify users' gesture when capturing touch operations? Here we need help from the gesturedetector. ongesturelistener interface. The Code is as follows:

Java code
  1. Public class mygesture extends activity implements ontouchlistener, ongesturelistener {
  2. Private gesturedetector mgesturedetector;
  3. Public mygesture (){
  4. Mgesturedetector =New gesturedetector (this );
  5. }
  6. Public void oncreate (bundle savedinstancestate ){
  7. Super. oncreate (savedinstancestate );
  8. Setcontentview (R. layout. Main );
  9. Textview TV = (textview) findviewbyid (R. Id. TV );
  10. TV. setontouchlistener (This );
  11. TV. setfocusable (True );
  12. TV. setclickable (True );
  13. TV. setlongclickable (True );
  14. Mgesturedetector. setislongpressenabled (True );
  15. }
  16. /*
  17. * In the ontouch () method, we call the ontouchevent () method of gesturedetector and deliver the captured motionevent to gesturedetector.
  18. * To analyze whether there is a proper callback function to process users' gestures
  19. */
  20. Public Boolean ontouch (view V, motionevent event ){
  21. Return mgesturedetector. ontouchevent (event );
  22. }
  23. // Touch the touch screen, triggered by one motionevent action_down
  24. Public Boolean ondown (motionevent arg0 ){
  25. Log. I ("Mygesture", "ondown ");
  26. Toast. maketext (This, "ondown", Toast. length_short). Show ();
  27. Return true;
  28. }
  29. /*
  30. * If you touch the touch screen, you have not released or dragged it. It is triggered by one motionevent action_down.
  31. * Pay attention to the difference from ondown (), emphasizing that the status is not released or dragged.
  32. */
  33. Public void onshowpress (motionevent e ){
  34. Log. I ("Mygesture", "onshowpress ");
  35. Toast. maketext (This, "onshowpress", Toast. length_short). Show ();
  36. }
  37. // The user (after touching the touch screen) is released and triggered by one motionevent action_up
  38. Public Boolean onsingletapup (motionevent e ){
  39. Log. I ("Mygesture", "onsingletapup ");
  40. Toast. maketext (This, "onsingletapup", Toast. length_short). Show ();
  41. Return true;
  42. }
  43. // The user presses the touch screen and moves quickly before releasing it. It is triggered by one motionevent action_down, multiple action_move, and one action_up.
  44. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
  45. Log. I ("Mygesture", "onfling ");
  46. Toast. maketext (This, "onfling", Toast. length_long). Show ();
  47. Return true;
  48. }
  49. // Press the touch screen and drag it. It is triggered by one motionevent action_down and multiple action_move operations.
  50. Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
  51. Log. I ("Mygesture", "onscroll ");
  52. Toast. maketext (This, "onscroll", Toast. length_long). Show ();
  53. Return true;
  54. }
  55. // The user presses the touch screen for a long time and is triggered by multiple motionevent action_down
  56. Public void onlongpress (motionevent e ){
  57. Log. I ("Mygesture", "onlongpress ");
  58. Toast. maketext (This, "onlongpress", Toast. length_long). Show ();
  59. }
  60. }

3. fling event processing code: Except for the first action_down that triggers fling and the coordinates contained in the last action_move, we can also use the user's moving speed on the X or Y axis as a condition. For example, in the following code, we can only process a user moving more than 100 pixels and moving more than 200 pixels per second on the X axis.

Java code
  1. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
  2. // Parameter explanation:
  3. // E1: 1st action_down motionevent
  4. // E2: The last action_move motionevent
  5. // Velocityx: moving speed on the X axis, pixel/second
  6. // Velocityy: The moving speed on the Y axis, pixel/second
  7. // Trigger condition:
  8. // The coordinate displacement of the X axis is greater than fling_min_distance, and the movement speed is greater than that of fling_min_velocity pixels/s.
  9. Final int fling_min_distance = 100, fling_min_velocity = 200;
  10. If (e1.getx ()-e2.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity ){
  11. // Fling left
  12. Log. I ("Mygesture", "fling left ");
  13. Toast. maketext (This, "fling left", Toast. length_short). Show ();
  14. }Else if (e2.getx ()-e1.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity ){
  15. // Fling right
  16. Log. I ("Mygesture", "fling right ");
  17. Toast. maketext (This, "fling right", Toast. length_short). Show ();
  18. }
  19. Return false;
  20. }

In this example,TV. setlongclickable ( True ) Is required because Only in this way can the view process be different from the hold (action_move, or multiple action_down) of the TAP (touch). We can also achieve this through Android: longclickable in the layout definition.

 

4. simpleongesturelistener

Java code
  1. Public class mygesture extends activity implements ontouchlistener {
  2. Private gesturedetector mgesturedetector;
  3. Public mygesture (){
  4. Mgesturedetector =New gesturedetector (New mysimplegesture ());
  5. }
  6. Public void oncreate (bundle savedinstancestate ){
  7. Super. oncreate (savedinstancestate );
  8. Setcontentview (R. layout. Main );
  9. Textview TV = (textview) findviewbyid (R. Id. TV );
  10. TV. setontouchlistener (This );
  11. TV. setfocusable (True );
  12. TV. setclickable (True );
  13. TV. setlongclickable (True );
  14. }
  15. Public Boolean ontouch (view V, motionevent event ){
  16. If (event. getaction () = motionevent. action_up ){
  17. Log. I ("Mygesture", "motionevent. action_up ");
  18. }
  19. Return mgesturedetector. ontouchevent (event );
  20. }
  21. // Simpleongesturelistener implements gesturedetector. ondoubletaplistener, gesturedetector. ongesturelistener
  22. Private class mysimplegesture extends simpleongesturelistener {
  23. // Triggered when the second touch is down.
  24. Public Boolean ondoubletap (motionevent e ){
  25. Log. I ("Mygesture", "ondoubletap ");
  26. Return super. ondoubletap (E );
  27. }
  28. // Both touch down and up will be triggered under double-click, which can be differentiated by E. getaction ().
  29. Public Boolean ondoubletapevent (motionevent e ){
  30. Log. I ("Mygesture", "ondoubletapevent ");
  31. Return super. ondoubletapevent (E );
  32. }
  33. // Triggered when touch is down
  34. Public Boolean ondown (motionevent e ){
  35. Log. I ("Mygesture", "ondown ");
  36. Return super. ondown (E );
  37. }
  38. // Triggered when up is triggered when a sliding distance is reached.
  39. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
  40. Log. I ("Mygesture", "onfling ");
  41. Return super. onfling (E1, E2, velocityx, velocityy );
  42. }
  43. // Triggered when touch is down without moving
  44. Public void onlongpress (motionevent e ){
  45. Log. I ("Mygesture", "onlongpress ");
  46. Super. onlongpress (E );
  47. }
  48. // Triggered when sliding is reached
  49. Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
  50. Log. I ("Mygesture", "onscroll ");
  51. Return super. onscroll (E1, E2, distancex, distancey );
  52. }
  53. /*
  54. * Triggered when touch is not sliding
  55. * (1) ondown must be triggered immediately as long as touch down
  56. * (2) after the touch is down, the onshowpress is triggered before the onlongpress is triggered.
  57. * So: do not slide after the touch is down. ondown-> onshowpress-> onlongpress is triggered in this order.
  58. */
  59. Public void onshowpress (motionevent e ){
  60. Log. I ("Mygesture", "onshowpress ");
  61. Super. onshowpress (E );
  62. }
  63. /*
  64. * Both functions are triggered when the touch is down without moving (onscroll) or long-pressed (onlongpress ).
  65. * Click very fast (do not slide) touch up: ondown-> onsingletapup-> onsingletapconfirmed
  66. * Click touch up: ondown-> onshowpress-> onsingletapup-> onsingletapconfirmed.
  67. */
  68. Public Boolean onsingletapconfirmed (motionevent e ){
  69. Log. I ("Mygesture", "onsingletapconfirmed ");
  70. Return super. onsingletapconfirmed (E );
  71. }
  72. Public Boolean onsingletapup (motionevent e ){
  73. Log. I ("Mygesture", "onsingletapup ");
  74. Return super. onsingletapup (E );
  75. }
  76. }
  77. }
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.