Android Gesturedetector Gesture Basics

Source: Internet
Author: User



1. When the user touches the screen, there will be many gestures, such as down,up,scroll,filing and so on, we know that the View class has a View.ontouchlistener internal interface, by rewriting his ontouch (View V, Motionevent event) method, we can handle some touch events, but this method is too simple, if you need to deal with some complex gestures, it will be cumbersome to use this interface (because we have to determine the user's touch on the trajectory of what is the gesture) Android The SDK provides us with the Gesturedetector (Gesture: Gesture Detector: Recognition) class, which allows us to identify a lot of gestures, mainly through his ontouchevent (event) method, to complete the identification of different gestures. Although he can identify gestures, but what to do with different gestures, should be provided to the programmer to implement, so this class provides two interfaces externally: Ongesturelistener,ondoubletaplistener, There is also an inner class Simpleongesturelistener,simpleongesturelistener class that Gesturedetector provides us with a more convenient response to different gesture classes, This class implements both interfaces (but all of the method bodies are empty), which is the static class, meaning that it is actually an external class. The programmer can inherit this class externally, overriding the gesture handling method inside.
The Simpleongesturelistener object can be passed in by Gesturedetector's construction method, so that gesturedetector can handle different gestures. 2. Specific usage: 2.1 Defaultgesturelistener.java
  1. Private class Defaultgesturelistener extends simpleongesturelistener{
  2. @Override
  3. public boolean onsingletapup (Motionevent e) {
  4. return false;
  5. }
  6. @Override
  7. public void Onlongpress (Motionevent e) {
  8. }
  9. /**
  10. * @param e1 The first down motion event, that started the scrolling.
  11. @param E2 The move motion event triggered the current onscroll.
  12. @param Distancex the distance along the X axis (axis) that have been scrolled since the last call to Onscroll. This is not the distance between E1 and E2.
  13. @param distancey the distance along the Y axis that had been scrolled since the last call to Onscroll. This is not the distance between E1 and E2.
  14. Whether you drag the view by hand, or by throwing the action to scroll, will be triggered many times, this method in the Action_move action occurs will trigger see Gesturedetector Ontouchevent method source code
  15. * */
  16. @Override
  17. public boolean onscroll (Motionevent E1, motionevent E2,
  18. Float Distancex, float distancey) {
  19. return false;
  20. }
  21. /**
  22. * @param E1 1th Action_down motionevent and only one
  23. * @param E2 last Action_move motionevent
  24. * @param movement speed on Velocityx x-axis, pixels/sec
  25. * @param movement speed on velocityy y axis, pixels/sec
  26. * This method will only trigger the Ontouchevent method to see Gesturedetector when it occurs in action_up.
  27. *
  28. * */
  29. @Override
  30. public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
  31. Float velocityy) {
  32. return false;
  33. }
  34. @Override
  35. public void Onshowpress (Motionevent e) {
  36. }
  37. @Override
  38. public boolean Ondown (Motionevent e) {
  39. return false;
  40. }
  41. @Override
  42. public boolean Ondoubletap (Motionevent e) {
  43. return false;
  44. }
  45. @Override
  46. public boolean ondoubletapevent (Motionevent e) {
  47. return false;
  48. }
  49. /**
  50. * This method is different from Onsingletapup, he is in Gesturedetector convinced that the user after the first touch of the screen, not immediately following the second touch screen, that is, not "double-click" When the trigger
  51. * */
  52. @Override
  53. public boolean onsingletapconfirmed (Motionevent e) {
  54. return false;
  55. }
  56. }
Copy Code 2.2 Public Gesturedetector (context context, Gesturedetector.ongesturelistener listener)//hand gesture response to gesture recognition class by construction method 2.3 In the Ontouch method of Ontouchlistener
    1. Private Ontouchlistener Gesturetouchlistener = new Ontouchlistener () {
    2. @Override
    3. public boolean OnTouch (View V, motionevent event) {
    4. Return Gdetector.ontouchevent (event);
    5. }
    6. };
Copy the code OK, it's over. 3. Case: Res.java
  1. Package com.android;
  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.widget.Button;
  9. public class Res extends Activity implements View.ontouchlistener {
  10. Button btn = null;
  11. Private Gesturedetector mgesturedetector = null;
  12. /** called when the activity is first created. */
  13. @Override
  14. public void OnCreate (Bundle savedinstancestate) {
  15. Super.oncreate (savedinstancestate);
  16. Setcontentview (R.layout.main);
  17. BTN = (Button) Findviewbyid (R.id.button);
  18. Btn.setontouchlistener (this);
  19. Mgesturedetector = new Gesturedetector (This, new Learngesturelistener ());
  20. }
  21. public Boolean OnTouch (view view, Motionevent event) {
  22. Return Mgesturedetector.ontouchevent (event);
  23. }
  24. Class Learngesturelistener extends Gesturedetector.simpleongesturelistener {
  25. @Override
  26. public boolean onsingletapup (Motionevent ev) {
  27. LOG.D ("DEBUG", "Onsingletapup");
  28. return true;
  29. }
  30. @Override
  31. public void onshowpress (Motionevent ev) {
  32. LOG.D ("DEBUG", "onshowpress");
  33. }
  34. @Override
  35. public void onlongpress (Motionevent ev) {
  36. LOG.D ("DEBUG", "onlongpress");
  37. }
  38. @Override
  39. public boolean onscroll (Motionevent E1, motionevent E2,
  40. Float Distancex, float distancey) {
  41. LOG.D ("DEBUG", "onscroll");
  42. return true;
  43. }
  44. @Override
  45. public boolean ondown (Motionevent ev) {
  46. LOG.D ("DEBUG", "Ondownd");
  47. return true;
  48. }
  49. @Override
  50. public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
  51. Float velocityy) {
  52. LOG.D ("DEBUG", "onfling");
  53. return true;
  54. }
  55. public boolean Ondoubletap (Motionevent event) {
  56. LOG.D ("DEBUG", "Ondoubletap");
  57. return true;
  58. }
  59. }
  60. }
Copy the code 4. Problems encountered:
1. Onfling (* *) cannot be triggered
By setting Mlistview.setlongclickable (TRUE), you can (I'm dealing with a ListView gesture event), so that the view is able to handle the hold (i.e. Action_move, which is different from tap) or multiple Action_down), we can also do this through the android:longclickable in the layout definition.
2. When the user presses the phone screen, the long-press event is triggered, and the up event is triggered when the screen is left, but Simpleongesturelistener does not provide an interface to the up event of the Longpress event
Workaround:
Similar to this, intercept up events, because all are ontouchlistener first obtained, and then passed to Simpleongesturelistener, here is a point to note:
After we have intercepted the up event and we have processed it, we have to give the event to Simpleongesturelistener processing, although we only intercept the up of the long-press event, but the Simpleongesturelistener has also done some processing for the long press event up , but there is no external interface available. What to do with it:
    1. if (minlongpress) {
    2. Mhandler.removemessages (TAP);
    3. Minlongpress = false;
    4. }
If the copy code is not given to simpleongesturelistener processing, then the click action will also trigger the Onlongpress method.
  1. Private Ontouchlistener Gesturetouchlistener = new Ontouchlistener () {
  2. @Override
  3. public boolean OnTouch (View V, motionevent event) {
  4. Switch (event.getaction ()) {
  5. Case Motionevent.action_down:
  6. Return Gdetector.ontouchevent (event);
  7. Case MOTIONEVENT.ACTION_UP:
  8. Mygesture.flaginfo info = Mgesture.getflaginfo ();
  9. if (info.isconnected==true) {
  10. int firstvisibleposition = Mlistview.getfirstvisibleposition ();
  11. View view = Mlistview.getchildat (info.position-firstvisibleposition);
  12. if (view!=null) {
  13. View.setbackgroundresource (R.drawable.listitem_background_blue);
  14. info.isconnected = false;
  15. }
  16. }
  17. Return Gdetector.ontouchevent (event);
  18. Case Motionevent.action_move:
  19. Return Gdetector.ontouchevent (event);
  20. }
  21. return false;
  22. }
  23. };
Copy Code Summary: 1. Click on the execution flow of an item on the screen there are two cases, one is short time, one time is slightly longer
Time is short: ondown--– "onsingletapup--–" onsingletapconfirmed
A little longer: ondown--– "onshowpress--" onsingletapup--– "onsingletapconfirmed
2. Long Press Events
ondown--– "onshowpress--" onlongpress
3. Toss: After the finger touches the screen, slightly swipe and release immediately
ondown-– "onscroll--" onscroll--"onscroll--" ...-–>onfling
4. Drag
ondown--"onscroll--" onscroll--"onfiling
Note: Sometimes it triggers onfiling, but sometimes it does not trigger, and personal understanding is caused by the non-standard movement of people.

Android Gesturedetector Gesture Basics

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.