Simple example explanation of Android gesture operation
The events provided by ontouch in the previous article are relatively simple. If we need to deal with some complex gestures, it will be very troublesome to use this interface, because we need to judge what gestures are according to the user's touch trajectory. Fortunately, the Android SDK provides us with the gesturedetector class, through which we can recognize many gestures, mainly through its ontouchevent (event) method. Gesturedetector provides two interfaces and an external class:
• interface: ongesturelistener, ondoubletaplistener • external 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.
Ongesturelistener has the following actions: • ondown (motionevent E): it will be triggered when the user presses the screen. • Onshowpress (motionevent E): if you press it for more than an instant, and it is not released or dragged when you press it, onshowpress will execute. I don't know how long the moment is... • onlongpress (motionevent E): long press the touch screen for more than a certain time, and this event will be triggered. Trigger sequence: ondown - > onshowpress - > onlongpress • onsingletapup (motionevent E): a single tap lift operation, that is, tap the screen and lift it immediately, will trigger this event. Of course, if there are other operations besides down, it will not be a single operation, so this event will not be triggered. Trigger sequence: click a very fast (no sliding) touchup: ondown - > onsingletapup - > onsingletapconfirmed click a slightly slower (no sliding) touchup: ondown - > onshowpress - > onsingletapup - > onsingletapconfirmed • onfling (motionevent E1, motionevent E2, float velocityx, float velocityy): slide the screen, and the user presses the touch screen After rapid movement, it is released by 1 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 the X axis, pixels / sec velocityy: movement speed on the Y axis, pixels / sec • onscroll (motionevent E1, float distancex, float distancey): drag events 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_ The move action is triggered when it occurs.
Ondoubletaplistener has the following actions: • 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 • 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 difference between onsingletapconfirmed and onsingletapup: onsingletapup will be executed as long as the hand is raised. For onsingletapconfirmed, if you double-click, onsingletapconfirmed will not be executed.
Use the gesture • use the gesturedetector.ongesturelistener interface
There are several steps to use the ongesturelistener interface: 1. Create the ongesturelistener listening function:
private class gestureListener implements GestureDetector.OnGestureListener{
}
2. Create a gesturedetector instance: 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);
Note: gesturedetector is in the status of detection, and gesturedetector compat is recommended
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener); GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener,Handler handler);
3. Intercept in ontouch (view v, motionevent event). In ontouch () method, we call the ontouchevent () method of gesturedetector to give the captured motionevent to gesturedetector to analyze whether there is an appropriate callback function to handle the user's gesture
4. Control binding
TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this);
Implementation code:
• the premise of implementing the ondoubletaplistener interface using the gesturedetector.ondoubletaplistener interface is to implement the ongesturelistener interface first. In fact, except for step 1, steps 2, 3 and 4 are exactly the same as those above. I won't repeat them. Let's look at step 1 below:
Create ongesturelistener and ondoubletaplistener listening functions at the same time: Method 1: create a new class and derive from ongesturelistener and ondoubletaplistener at the same time:
Method 2: use gesturedetector. Setondoubletaplistener(); Function setting listening:
Note: you can see that both method 1 and method 2 need to be derived from gesturedetector.ongesturelistener. We mentioned the constructor of gesturedetectorcompat earlier, as follows:
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,Handler handler);
As you can see, both of its constructor parameters must be instances of ongesturelistener. Therefore, if you want to use several functions of ondoubletaplistener, you must first implement ongesturelistener. Implementation code:
• use the gesturedetector.simpleongesturelistener class
Use the ongesturelistener and ondoubletaplistener interfaces. This requires overloading all methods of the interface and is suitable for listening to all gestures. If we only want to listen to a gesture or several gestures, we can use the simpleongesturelistener class at this time. 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.
Source code download: http://xiazai.jb51.net/201609/yuanma/GestureDetector (jb51.net).rar
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.