Use of Android Gesture listener class Gesturedetector

Source: Internet
Author: User

When using a custom view, the handling of touch-screen events is more than necessary, the ability to write their own code processing, so more flexible. If you don't want to be so troublesome, Android provides a gesture listener class gesturedetector that we can use. Gesturedetector is easy to use, provides a click, double click, long press and other operations, but the general definition of the interface is more complex, but also with a lot of attention to the place, here to summarize the use of gesturedetector.

First create a blank project, the main interface layout only need to add a button on the line

  1. <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  2. Xmlns:tools= "Http://schemas.android.com/tools"
  3. Android:layout_width= "Match_parent"
  4. android:layout_height= "Match_parent"
  5. android:paddingbottom= "@dimen/activity_vertical_margin"
  6. android:paddingleft= "@dimen/activity_horizontal_margin"
  7. android:paddingright= "@dimen/activity_horizontal_margin"
  8. android:paddingtop= "@dimen/activity_vertical_margin"
  9. Tools:context= ". Mainactivity ">
  10. <button
  11. Android:id= "@+id/btn_textgesture"
  12. Android:layout_width= "Fill_parent"
  13. android:layout_height= "Fill_parent"
  14. android:text= "@string/app_name"/>
  15. </RelativeLayout>
Copy the code due to the touch screen event to be tested, all this button is larger, the main interface is as follows: <ignore_js_op> first introduces the basic idea of touch screen event processing. Touch screen generally has three basic events, down press, move moves, up left, through the three basic events monitoring, to determine what the user performed the operation. A standard touch screen operation is generally a combination of a series of basic events, in the Android framework, through the Ontouch () function to obtain basic touch-screen events, and functions like the onclick, is already a series of basic events combination. For example, a down event occurred, no move event occurred before the up event, or the range of the move was small, and the down and up events were short intervals, which is a click or Singeltap event that contrasts the event of the physical keyboard key, The physical keyboard operates after the down event, and the touch-screen event is typically done after the up event occurs. Here's the code for the activity.
  1. Package com.example.testgesture;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.util.Log;
  5. Import Android.view.GestureDetector;
  6. Import Android.view.GestureDetector.SimpleOnGestureListener;
  7. Import android.view.MotionEvent;
  8. Import Android.view.View;
  9. Import Android.view.View.OnTouchListener;
  10. Import Android.widget.Button;
  11. public class Mainactivity extends Activity {
  12. Private Button Mbutton;
  13. Private Gesturedetector Mgesturedetector;
  14. @Override
  15. protected void OnCreate (Bundle savedinstancestate) {
  16. Super.oncreate (savedinstancestate);
  17. Setcontentview (R.layout.activity_main);
  18. Mgesturedetector = new Gesturedetector (This, new Myongesturelistener ());
  19. Mbutton = (Button) Findviewbyid (r.id.btn_textgesture);
  20. Mbutton.setontouchlistener (New Ontouchlistener () {
  21. @Override
  22. public boolean OnTouch (View V, motionevent event) {
  23. LOG.I (GetClass (). GetName (), "onTouch-----" + getactionname (Event.getaction ()));
  24. Mgesturedetector.ontouchevent (event);
  25. Be sure to return true, otherwise you will not get the full event
  26. return true;
  27. }
  28. });
  29. }
  30. Private String getactionname (int action) {
  31. String name = "";
  32. Switch (action) {
  33. Case Motionevent.action_down: {
  34. Name = "Action_down";
  35. Break
  36. }
  37. Case Motionevent.action_move: {
  38. Name = "Action_move";
  39. Break
  40. }
  41. Case MOTIONEVENT.ACTION_UP: {
  42. Name = "Action_up";
  43. Break
  44. }
  45. Default
  46. Break
  47. }
  48. return name;
  49. }
  50. Class Myongesturelistener extends Simpleongesturelistener {
  51. @Override
  52. public boolean onsingletapup (Motionevent e) {
  53. LOG.I (GetClass (). GetName (), "onsingletapup-----" + getactionname (E.getaction ()));
  54. return false;
  55. }
  56. @Override
  57. public void Onlongpress (Motionevent e) {
  58. LOG.I (GetClass (). GetName (), "onlongpress-----" + getactionname (E.getaction ()));
  59. }
  60. @Override
  61. public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {
  62. LOG.I (GetClass (). GetName (),
  63. "Onscroll-----" + getactionname (e2.getaction ()) + ", (" + e1.getx () + "," + e1.gety () + "), ("
  64. + e2.getx () + "," + e2.gety () + ")");
  65. return false;
  66. }
  67. @Override
  68. public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {
  69. LOG.I (GetClass (). GetName (),
  70. "onfling-----" + getactionname (e2.getaction ()) + ", (" + e1.getx () + "," + e1.gety () + "), ("
  71. + e2.getx () + "," + e2.gety () + ")");
  72. return false;
  73. }
  74. @Override
  75. public void Onshowpress (Motionevent e) {
  76. LOG.I (GetClass (). GetName (), "onshowpress-----" + getactionname (E.getaction ()));
  77. }
  78. @Override
  79. public boolean Ondown (Motionevent e) {
  80. LOG.I (GetClass (). GetName (), "ondown-----" + getactionname (E.getaction ()));
  81. return false;
  82. }
  83. @Override
  84. public boolean Ondoubletap (Motionevent e) {
  85. LOG.I (GetClass (). GetName (), "ondoubletap-----" + getactionname (E.getaction ()));
  86. return false;
  87. }
  88. @Override
  89. public boolean ondoubletapevent (Motionevent e) {
  90. LOG.I (GetClass (). GetName (), "ondoubletapevent-----" + getactionname (E.getaction ()));
  91. return false;
  92. }
  93. @Override
  94. public boolean onsingletapconfirmed (Motionevent e) {
  95. LOG.I (GetClass (). GetName (), "onsingletapconfirmed-----" + getactionname (E.getaction ()));
  96. return false;
  97. }
  98. }
  99. }
The copy code first declares a gesturedetector, and then overrides the button's Ontouch function, handing the touchscreen event to gesturedetector processing. First do a click on the button to do a  <ignore_js_op>onsingletapup is called, stating that a Click event occurred, Onsingletapconfirmed is called, the description confirms that a Click event has occurred, is not a double-click event. It is important to note that Onsingletapup is already a click event and the Onsingletapup trigger is the Action_up event. Onsingletapconfirmed is triggered when the user's finger leaves the screen, and all up is not the end of all touchscreen events. Do a double-click Operation <ignore_js_op>  A onsingletapup occurred first, stating that a Click event was completed, and then Ondoubletap occurred, and a double-click event has been completed. As we can see, ondoubletap occurs when the Action_down event, which means that the double-click event is triggered when the second time the screen is pressed, not the second time it leaves the screen, after the ondoubletap occurs, You can hear all the touch events from the press to bounce in the ondoubletapevent when the double-click event occurs. Onsingletapup and onsingletapconfirmed are not triggered after ondoubletap occurs. Do a long press operation <ignore_js_op> onlongpress is really action_down occurs, onlongpress occurs after the up will not be triggered with other events, You can change the status of the onshowpress, such as the pressed state of the button. Do a sliding operation <ignore_js_op> onscroll event is drag, Onfling is thrown. Combine log to get a look. First is the Action_down, after several action_move, moved more than a certain distance, triggered the onscroll, if the onscroll is triggered, there is no long press before the up, click, double-clicking events. Take a look at the parameters of Onscroll
    1. public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey)
The copy code E1 for the first pressed event, as in the Ondown event, E2 is the current event, Distancex the x-axis distance for the onscroll movement, Distancey for the moving y-axis distance, The distance traveled is relative to the last onscroll event, not the distance between the current point and the point at which it was pressed. This slide finally triggered the Onfling event, but the Onfling event trigger is not certain, onfling is triggered in action_up, the usual list in the left screen is to continue scrolling, is triggered by this way.
    1. public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy)
The first two parameters of the copy code onfling are the same as the onscroll, E2 the point at which the user leaves the screen after dragging. Veloctiyx,velocity for the initial speed when leaving the screen, at these two speeds for the initial speed of the uniform deceleration movement, is now drag the list and drag the picture of the various cache scrolling effect. The return value of the function has a return value in addition to Onlongpress,
    1. Mbutton.setontouchlistener (New Ontouchlistener () {
    2. @Override
    3. public boolean OnTouch (View V, motionevent event) {
    4. LOG.I (GetClass (). GetName (), "onTouch-----" + getactionname (Event.getaction ()));
    5. Mgesturedetector.ontouchevent (event);
    6. Be sure to return true, otherwise you will not get the full event
    7. return true;
    8. }
    9. });
Copy code These return values are passed Mgesturedetector.ontouchevent (event); Passed to Ontouch.
Finally summing up the gesturedetector combined with Simpleongesturelistener can easily get to click, double click, long Press and other events, but the handling of these events is not simple in the corresponding function to do some operations can be, Complex custom views are still in Ontouch to judge the focus state of the control, and Gesturedetector is not omnipotent, if you want to handle the long press after the move, it will take a lot of effort, Think Gesturedetector in the long press after the occurrence is not in the onscroll, you can only through the Ontouch inside the action_move processing.

Use of Android Gesture listener class Gesturedetector

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.