Android gesturedetector user gesture detection example explanation

1、 Overview

When the user touches the screen, many gestures will be generated, such as down, up, scroll, filling and so on. Generally, we know that the view class has a view.ontouchlistener internal interface. By overriding its ontouch (view v, motionevent event) method, we can handle some touch events, but this method is too simple. If we need to handle some complex gestures, it will be very troublesome to use this interface (because we have to judge what gesture is according to the trajectory touched by the user).

Android SDK provides us with gesturedetector (gesture: gesture detector: recognition) class. Through this class, we can recognize many gestures, mainly through its ontouchevent (event) method. Although he can recognize gestures, how to deal with different gestures should be provided to programmers.

The gesturedetector class provides two external interfaces and an external class interface: ongesturelistener, ondoubletaplistener, and the internal class: simpleongesturelistener

This external class is actually the integration of all functions in the two interfaces. It contains all the functions that must be implemented in the two interfaces and has been rewritten, but all method bodies are empty; The difference is that this class is static class. Programmers can inherit this class externally and rewrite the gesture processing methods inside.

Let's first look at the ongesturelistener interface;

2、 Gesturedetector. Ongesturelistener --- interface

1. Basic explanation: if we write a class and implement ongesturelistener, we will be prompted that there are several functions that must be overridden. After that, it looks like this:

It can be seen that a total of six functions have been rewritten here. Under what circumstances will these functions be triggered? Let's talk about it below:

Ondown (motionevent E): triggered when the user presses the screen; Onshowpress (motionevent E): if it is pressed for more than an instant, and it is not released or dragged when pressed, onshowpress will execute. I don't know how long this moment is. Er... Onlongpress (motionevent E): long press the touch screen for more than a certain time will trigger this event

Trigger sequence: ondown - > onshowpress - > onlongpress onsingletapup (motionevent E): it can also be seen from the name that a single tap lift operation, that is, tap the screen and lift it up immediately, will trigger this event. Of course, if there are other operations besides down, it is no longer a single operation, so this event will not be triggered

Trigger sequence: click the very fast (no sliding) touchup: ondown - > onsingletapup - > onsingletapconfirmed, and click the slightly slower (no sliding) touchup: ondown - > onshowpress - > onsingletapup - > onsingletapconfirmed

Onfling (motionevent E1, float velocityy): slide the screen. The user presses the touch screen and releases it after rapid movement. It is controlled by one motionevent action_ Down, multiple actions_ Move, 1 action_ Up trigger

Parameter explanation: E1: the first action_ Down motionevent E2: last action_ Move motionevent velocityx: movement speed on X axis, pixels / sec velocityy: movement speed on Y axis, pixels / sec

Onscroll (motionevent E1, float distance y): drag an event on the screen. Whether you drag the view by hand or roll by throwing, it will be triggered many times. This method is in action_ Triggered when the move action occurs

Sliding screen: after touching the screen with your finger, immediately release ondown ------ "onscroll ------" onscroll ------ "onscroll ------"... ------ > onfiling drag: ondown ------ "onscroll ------" onscroll ------ "onfiling

It can be seen that no matter sliding or dragging, it only affects the number of onscroll triggers in the middle, and the onfling event will eventually be triggered!

2. Instance

There are three steps to use gesturedetector: 1. Create ongesturelistener listener listening function: you can use to construct an instance:

You can also construct classes:

2. Create a gesturedetector instance mgesturedetector:

There are three constructors, which can be selected as needed:

GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener); GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

3. Blocking in ontouch (view v, motionevent event):

4. Control binding

Now enter the instance stage: first, add a textview on the main layout page and enlarge it to the whole screen to facilitate gesture recognition. The code is:

Then, in the Java code, write the code according to the above three-step principle, and add toast prompt and log under all gestures

The source code is given at the bottom of the blog.

3、 Gesturedetector.ondoubletaplistener --- interface

1. Build

There are two ways to set double click listening:

Method 1: create a new class derived from ongesturelistener and ondoubletaplistener at the same time:

Method 2: use gesturedetector:: setondoubletaplistener(); Function setting listening:

Note: you can see that in both method 1 and method 2, you need to derive from gesturedetector. Ongesturelistener. We mentioned the constructor of gesturedetector earlier, as follows: gesturedetector gesturedetector = new gesturedetector (gesturedetector. Ongesturelistener listener listener); GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

You can see that in the constructor, except for simpleongesturelistener to be discussed later, the other two constructors must be instances of ongesturelistener. Therefore, if you want to use several functions of ondoubletaplistener, you must first implement ongesturelistener.

2. Function explanation

First, take a look at the three functions that the ondoubletaplistener interface must override:

Onsingletapconfirmed (motionevent E): click the event. It is used to determine that the click is singletap instead of doubletap. If you click twice in a row, it is a doubletap gesture. If you click only once and the system does not receive the second click after waiting for a period of time, it will determine that the click is singletap instead of doubletap, and then trigger the singletapconfirmed event. The trigger sequence is: ondown - > onsingletapup - > onsingletapconfirmed. There is a difference between onsingletapconfirmed and onsingletapup: ongesturelistener has such a method, onsingletapup, which is easy to be confused with onsingletapconfirmed. The difference between the two is: onsingletapup will be executed as long as the hand is raised. For onsingletapconfirmed, if you double-click, onsingletapconfirmed will not be executed. Ondoubletap (motionevent E): double click the event

Ondoubletapevent (motionevent E): double click the action that occurs in the interval. It refers to other actions that occur between double clicks after ondoubletap is triggered, including down, up and move events; The following figure shows the log output after double clicking:

Two points are summarized:

1. As can be seen from the above figure, ondoubletap is triggered first, and then ondown is triggered for the second click

2. Secondly, after ondoubletap is triggered, ondoubletapevent will be triggered. The number after ondoubletapevent represents the current event, and 0 refers to action_ Down, 1 refers to action_ Up, 2 refers to action_ Move on the basis of the previous example, we add a double-click listening class, which is implemented as follows:

Double click, and some screenshots are as follows:

Double click the corresponding trigger event sequence:

Click once, and the corresponding event triggering sequence is:

The source code is given at the bottom of the blog.

4、 Gesturedetector.simpleongesturelistener --- class

It is different from the first two: 1. This is a class. If you create a new class based on it, you should use extensions instead of implements! 2. The functions in the ongesturelistener and ondoubletaplistener interfaces must be overridden, that is, an empty function must be overridden before use, but this is not necessary in the instance or derived class of simpleongesturelistener class. You can override which function you use according to the situation, Because the simpleongesturelistener class itself has implemented all the functions of the two interfaces, but it is all empty. The simpleongesturelistener class is used to re implement the above effects. The code is as follows:

Now that all the basic knowledge about gesturedetector has been explained, here is a small application - identifying whether users slide left or right!

The source code is given at the bottom of the blog.

5、 Onfling application -- identify whether to slide left or right

This part is interesting. It can be said to be a small application of the above knowledge. We use onfling function to identify whether the current user is sliding left or right, so as to print the log. Take a look at onfling parameters first:

boolean onFling(MotionEvent e1,float veLocityY)

Parameter explanation: E1: the first action_ Down motionevent E2: last action_ Move motionevent velocityx: the moving speed on the x-axis, pixels / sec velocityy: the moving speed on the y-axis, pixels / sec. First, let's talk about the realized functions: when the user slides to the left for more than 100px and the sliding speed exceeds 100 PX / s, it is judged as sliding to the left; The same applies to the right. The code is as follows:

This code is not difficult, so I won't talk about it in detail. Let's see the effect:

The source code is given at the bottom of the blog.

Source address: gesturedetector gesture detection

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>