Use of GestureDetector for Android gesture listening

Source: Internet
Author: User
Tags gety

When using a custom view, the processing of touch screen events is essential, and the ability to write code for processing is more flexible. If you don't want to be so troublesome, Android provides a gesture listening class GestureDetector, which can be used by us. GestureDetector is very convenient to use. It provides operations such as click, double-click, and long-press. However, the general definition interface is complicated and requires a lot of attention, here we will summarize the usage of GestureDetector.

First, create a blank project. You only need to add a button in layout of the main interface.

                               

Because of the touch screen events to be tested, all the buttons are relatively large and the main interface is as follows:

First, we will introduce the basic ideas for processing touch screen events. The Touch Screen usually has three basic events: down and down, move and move, and up. By listening to these three basic events, you can determine what operations the user has performed. A standard touch screen operation is generally a combination of a series of basic events. In the Android framework, the onTouch () function can be used to obtain basic touch screen events, while functions such as onClick, it is already a combination of a series of basic events.

For example, if a Down event occurs, there is no move event before the up event, or the range of the move event is small, and the interval between the down event and the up event is very short, this is a click or singelTap event,

Compared with the events of buttons on the keyboard, the keyboard is operated after the down event, while the touch screen event is usually performed after the up event.

The following is the activity code.

               MainActivity                                        mGestureDetector =  GestureDetector(,           mButton =         mButton.setOnTouchListener(                                 Log.i(getClass().getName(), "onTouch-----" +                                              String getActionName(         String name = ""                                       name = "ACTION_DOWN"                                                name = "ACTION_MOVE"                                                name = "ACTION_UP"                                                              MyOnGestureListener                         Log.i(getClass().getName(), "onSingleTapUp-----" +                                        Log.i(getClass().getName(), "onLongPress-----" +              onScroll(MotionEvent e1, MotionEvent e2,  distanceX,                       "onScroll-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("                             + e2.getX() + "," + e2.getY() + ")"                            onFling(MotionEvent e1, MotionEvent e2,  velocityX,                       "onFling-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("                             + e2.getX() + "," + e2.getY() + ")"                                        Log.i(getClass().getName(), "onShowPress-----" +                          Log.i(getClass().getName(), "onDown-----" +                                        Log.i(getClass().getName(), "onDoubleTap-----" +                                        Log.i(getClass().getName(), "onDoubleTapEvent-----" +                                        Log.i(getClass().getName(), "onSingleTapConfirmed-----" +                 }

 

First, declare a GestureDetector, then rewrite the onTouch function of the Button, and hand over the touch screen event to the GestureDetector for processing.

First, click the button.

OnSingleTapUp is called, indicating that a click event occurs and onSingleTapConfirmed is called. This indicates that a click event is confirmed, not a double-click event. Note that onSingleTapUp is already a click event. When onSingleTapUp is triggeredACTION_UPEvent. OnSingleTapConfirmed is in the user's fingerExit ScreenAfter the event is triggered, not all the up events are completed.

Double-click

An onSingleTapUp event occurs first, indicating that a click event is completed, and then an onDoubleTap occurs. So far, a double-click event has been completed. We can see that onDoubleTap occursACTION_DOWNThe double-click event is triggered when the screen is pressed for the second time, instead of when the screen is left for the second time. After onDoubleTap occurs, in onDoubleTapEvent, You can monitor all touch-screen events that start from press to pop-up after the double-click event occurs. OnDoubleTap does not trigger onSingleTapUp and onSingleTapConfirmed when it occurs.

Perform a long-pressed operation

OnLongPressACTION_DOWNWhen onLongPress occurs, it will not be triggered by other events before the up. You canOnShowPressChanges the status, such as the button press status.

Perform a slide operation

The onScroll event is drag, and the onFling event is throw. Take a look at the log. The first is ACTION_DOWN, and then multiple ACTION_MOVE operations. Moving beyond a certain distance triggers onScroll. If onScroll is triggered,There will be no long press, click, double-click or other events before the up. Take a look at the onScroll parameters.

  onScroll(MotionEvent e1, MotionEvent e2,  distanceX,  distanceY)

E1 is the first press event. Like in the onDown event, e2 is the current event, distanceX is the X-axis distance of the onScroll movement, and distanceY is the y-axis distance of the movement, the moving distance is the moving distance relative to the last onScroll event, rather than the distance between the current point and the pressed point.

This slide finally triggers the onFling event, but the onFling event is not triggered in a certain way. The onFling event is triggered in ACTION_UP, And the list continues to scroll when it leaves the screen.

  onFling(MotionEvent e1, MotionEvent e2,  velocityX,  velocityY) 

The first two parameters of onFling are the same as those of onScroll. e2 is the point when you drag the screen. VeloctiyX and velocitY are the initial speeds when you exit the screen. They take these two speeds as the initial speeds for smooth deceleration. They are the effects of dragging the list and dragging the image cache to scroll.

Function return value

Except onLongPress, these functions return values,

         mButton.setOnTouchListener(                                 Log.i(getClass().getName(), "onTouch-----" +                                              });


These return values are passed to onTouch through mGestureDetector. onTouchEvent (event.

Summary

GestureDetector combined with SimpleOnGestureListener can easily obtain click, double-click, long-press, and other events, but the processing of these events is not simply done in the corresponding function, in a complicated custom view, you still need to judge the focus status of each control in onTouch, and GestureDetector is not omnipotent. If you want to process the movement after long-pressed, it will take some effort, it is assumed that GestureDetector will not have onScroll when long-pressed occurs. You can only process it through ACTION_MOVE in onTouch.

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.