Android gesturedetector Gesture Recognition class

Source: Internet
Author: User

To enhance the mouse response to events, Android provides the gesturedetector Gesture Recognition class. Gesturedetector. ongesturelistener is used to obtain the currently triggered operation gestures (single tap up, show press, long press, scroll, down, and fling), including the following:

Boolean ondoubletap (motionevent E)
Explanation: triggered when the second touch is down under double-click
Boolean ondoubletapevent (motionevent E)
Explanation: Double-click triggers both touch down and up, which can be distinguished by E. getaction.
Boolean ondown (motionevent E)
Explanation: triggered when touch is down
Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy)

Explanation: Touch is triggered when it is up after sliding a little distance.
Void onlongpress (motionevent E)
Explanation: triggered when touch is down without moving
Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey)

Explanation: It is triggered when sliding is reached.
Void onshowpress (motionevent E)
Explanation: triggered when touch is not sliding
(Compared with ondown and onlongpress
Ondown must be triggered immediately as long as touch down.
After touchdown, onshowpress is triggered before onlongpress after a while without sliding.
Therefore, it does not slide after touchdown. ondown-> onshowpress-> onlongpress is triggered in this order.

Boolean onsingletapconfirmed (motionevent E)
Boolean onsingletapup (motionevent E)
Explanation: the above two functions are triggered when the touch is down without moving (onscroll) or long-pressed (onlongpress.

Click very fast (do not slide) touchup:
Ondown-> onsingletapup-> onsingletapconfirmed
Click a slightly slower (do not slide) touchup:
Ondown-> onshowpress-> onsingletapup-> onsingletapconfirmed

To use gesturedetector, You need to override the ontouchevent event in the view. For example:

[Java]
View plaincopyprint?
  1. Gesturedetector mgesture = NULL;
  2. @ Override
  3. Public Boolean ontouch (view V, motionevent event)
  4. {
  5. // Todo auto-generated method stub
  6. Return mgesture. ontouchevent (event );
  7. }

Gesturedetector mgesture = NULL; <br/> @ override <br/> Public Boolean ontouch (view V, motionevent event) <br/>{< br/> // todo auto-generated method stub <br/> return mgesture. ontouchevent (event); <br/>}

A detailed test example is as follows:

[CPP]
View plaincopyprint?
  1. Package com. jiubang. Android. gesturetest;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. util. log;
  5. Import Android. View. gesturedetector;
  6. Import Android. View. motionevent;
  7. Import Android. View. view;
  8. Import Android. View. gesturedetector. simpleongesturelistener;
  9. Import Android. View. View. ontouchlistener;
  10. Import Android. widget. Button;
  11. Public class gestureactivity extends Activity
  12. Implements ontouchlistener
  13. {
  14. Private button mbutton = NULL;
  15. Gesturedetector mgesture = NULL;
  16. /** Called when the activity is first created .*/
  17. @ Override
  18. Public void oncreate (bundle savedinstancestate ){
  19. Super. oncreate (savedinstancestate );
  20. Setcontentview (R. layout. Main );
  21. Log. I ("test", "oncreate ");
  22. Mbutton = (button) findviewbyid (R. Id. button1 );
  23. Mbutton. setontouchlistener (this );
  24. Mgesture = new gesturedetector (this,
    New gesturelistener ());
  25. }
  26. @ Override
  27. Public Boolean ontouch (view V, motionevent event)
  28. {
  29. // Todo auto-generated method stub
  30. Return mgesture. ontouchevent (event );
  31. }
  32. Class gesturelistener extends simpleongesturelistener
  33. {
  34. @ Override
  35. Public Boolean ondoubletap (motionevent E)
  36. {
  37. // Todo auto-generated method stub
  38. Log. I ("test", "ondoubletap ");
  39. Return super. ondoubletap (E );
  40. }
  41. @ Override
  42. Public Boolean ondown (motionevent E)
  43. {
  44. // Todo auto-generated method stub
  45. Log. I ("test", "ondown ");
  46. Return super. ondown (E );
  47. }
  48. @ Override
  49. Public Boolean onfling (motionevent E1, motionevent E2,
    Float velocityx,
  50. Float velocityy)
  51. {
  52. // Todo auto-generated method stub
  53. Log. I ("test", "onfling: velocityx =" + velocityx +
    "Velocityy" + velocityy );
  54. Return super. onfling (E1, E2, velocityx, velocityy );
  55. }
  56. @ Override
  57. Public void onlongpress (motionevent E)
  58. {
  59. // Todo auto-generated method stub
  60. Log. I ("test", "onlongpress ");
  61. Super. onlongpress (E );
  62. }
  63. @ Override
  64. Public Boolean onscroll (motionevent E1, motionevent E2,
  65. Float distancex, float distancey)
  66. {
  67. // Todo auto-generated method stub
  68. Log. I ("test", "onscroll: distancex =" + distancex +
    "Distancey =" + distancey );
  69. Return super. onscroll (E1, E2, distancex, distancey );
  70. }
  71. @ Override
  72. Public Boolean onsingletapup (motionevent E)
  73. {
  74. // Todo auto-generated method stub
  75. Log. I ("test", "onsingletapup ");
  76. Return super. onsingletapup (E );
  77. }
  78. }
  79. }

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.