When I first started learning Android, I felt that Google's documents were not good, and when we studied gestures, I felt that Google's documents were too poor. A lot of constants, properties, and methods don't even have a description. There is no description, but there are so many gestures in ongesturelistener, it does not have a description, who can understand onlongpress and onshowpress before they try.
Is there a difference between onscroll and onfling? Google really needs to do a big surgery on the documentation. But fortunately, after my repeated attempts. These gestures are defined from a personal point of view.
Press (Ondown): Just the moment the finger touches the touch screen, it is the touch of the moment.
Throw (onfling): The finger moves quickly on the touchscreen and releases the action.
Long Press (onlongpress): The finger is pressed for a period of time and is not loosened.
Scrolling (onscroll): Fingers slide on the touchscreen.
Press and Hold (onshowpress): The finger is pressed on the touchscreen, its time range is pressed, and before the long press.
Lift (Onsingletapup): The moment the finger leaves the touch screen.
In addition to these definitions, I also summed up a bit of experience, and here to share with you.
Any gesture action will first perform a pressing (Ondown) action.
Press and hold (onshowpress) action before long pressing (onlongpress) action.
A lift (Onsingletapup) action is performed after holding (onshowpress) the action and pressing (Ondown) the action.
Hold (onlongpress), scroll (onscroll), and throw (onfling) actions do not perform a lift (onsingletapup) action.
Android Recognition touch screen gestures make the user experience much more. There is a View.ontouchlistener internal interface in the view class, and by overriding his Ontouch (View V, motionevent event) method, we can handle some simple touch events, but this method does not recognize gestures, If you need to deal with some complex gestures, it can be cumbersome to use this interface (because we want to determine what gesture is based on the trajectory of the user's touch). Fortunately Android provides us with the Gesturedetector class, through which we can easily do gesture recognition. Below I make a brief introduction.
I. Gesturedetector INTRODUCTION
1. Composition
The Gesturedetector class is used to identify the various gestures of the touchscreen, which contains two interfaces and an inner class:
Interface:
Ongesturelistener: Used to listen for gesture events (6 kinds).
Ondoubletaplistener: Used to listen for double-click events.
Inner class:
Simpleongesturelistener: Used to listen for all gestures. In fact, it implements the above two interfaces, but the method body is empty, we need to write ourselves. We can inherit this class and rewrite the method inside to do gesture processing.
2. Construction
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. Methods
(1) ontouchevent (motionevent ev) analysis captures a touch event triggering the corresponding callback function
(2) setislongpressenabled (Boolean islongpressenabled) setting "Long Press" is available
(3) Setondoubletaplistener (Gesturedetector.ondoubletaplistener Ondoubletaplistener) set double-click Listener
4. Use
Process:
First, the system captures the touch event of the screen (Ontouchlistener), which does not involve a specific gesture, but simply captures the touch.
Next, call Gesturedetector's Ontouchevent () method in the Ontouch () method to give the captured motionevent to Gesturedetector to handle
Finally, you need to implement an abstract method.
Realize:
(1) Create Gesturedetector instance gesturedetector in activity.
(2) can choose according to the need:
Rewrite the Ongesturelistener and pass in the Gesturedetector through the constructor function
Override Ondoubletaplistener and pass Gesturedetector.setondoubletaplistener method to Gesturedetector
Rewrite the Simpleongesturelistener and pass in the Gesturedetector through the constructor function
(3) Rewrite the activity's Ontouchevent method, give all touch events to Gesturedetector to handle
@Override
public boolean ontouchevent (Motionevent event) {
Return Gesturedetector.ontouchevent (event);
}
Two. Ongesturelistener
The 1.onGestureListener recognizes 6 gestures, namely:
(1) Ondown (motionevent e): down event;
(2) Onsingletapup (motionevent e): one click Up event;
(3) onshowpress (motionevent e): A Down event occurs while move or up has not occurred before the event is triggered;
(4) onlongpress (motionevent E): Long press events;
(5) Onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy): swipe gesture event;
(6) Onscroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag the event on the screen.
Here I need to verbose on the internet to find out about onfling and onscroll a little difference.
Onfling () is a fling, which is performed when a motionevent.action_up (finger lift) occurs, and onscroll () is executed as long as the finger moves. He will not execute motionevent.action_up. Onfling are often used for page flipping, while onscroll is typically used to zoom in and out and move.
2. Rewrite
Ongesturelistener ongesturelistener=new Ongesturelistener () {
@Override
public boolean Ondown (Motionevent e) {
return false;
}
@Override
public boolean onfling (Motionevent E1, motionevent E2,
Float Velocityx, float velocityy) {
return false;
}
@Override
public boolean onlongpress (Motionevent e) {
return false;
}
@Override
public boolean onscroll (Motionevent E1, motionevent E2,
Float Distancex, float distancey) {
return false;
}
@Override
public void Onshowpress (Motionevent e) {
}
@Override
public boolean onsingletapup (Motionevent e) {
return false;
}
}
You can add specific processing methods to the function as needed. Then pass in the Gesturedetector through the constructor function.
Gesturedetector gesturedetector=new Gesturedetector (This,ongesturelistener);
Two. Ondoubletaplistener
1.OnDoubleTapListener is used to detect the mouse double-click event. The abstract methods that need to be implemented are:
(1) Ondoubletap (motionevent e): Double-click the event.
(2) ondoubletapevent (motionevent E): Additional actions occur in the double-click interval. Notifies events in the Doubletap gesture, including down, up, and move events
(This refers to an event that occurs between double-clicking, such as a doubletap gesture in the same place, and a down and up event in the Doubletap gesture, both of which are notified by the function);
(3) onsingletapconfirmed (motionevent e): Click event. Used to determine if the click Is Singletap instead of Doubletap, and if the two consecutive clicks are doubletap gestures,
If you click only once, the system waits for a period of time and does not receive a second click to determine that the click is Singletap instead of DOUBLETAP and then triggers the Singletapconfirmed event.
A little bit different about onsingletapconfirmed and Onsingletapup: Ongesturelistener has such a way of Onsingletapup, and onsingletapconfirmed is easy to confuse.
The difference between the two is: Onsingletapup, as long as the hand is lifted up will be executed, and for onsingletapconfirmed, if the double-click, then onsingletapconfirmed will not execute.
2. Rewrite
Ondoubletaplistener Ondoubletaplistener New Ondoubletaplistener () {
@Override
public boolean onsingletapconfirmed (Motionevent e) {
return false;
}
@Override
public boolean ondoubletapevent (Motionevent e) {
return false;
}
@Override
public boolean Ondoubletap (Motionevent e) {
return false;
}
)
You can add specific processing methods to the function as needed, and then pass the Setondoubletaplistener to the Gesturedetector.
Gesturedetector.setondoubletaplistener (Ondoubletaplistener);
Three. Simpleongesturelistener
Simpleongesturelistener is an inner class of the Gesturedetector class, which is a static class, which means that it is actually an outer one. This class can be inherited externally, overriding the gesture handling method inside.
1.SimpleOnGestureListener actually implements Ongesturelistener and Ondoubletaplistener, so it can do all of the above mentioned gesture recognition (9 kinds), as described above.
2. Rewrite
public class Simpleongesturelistener extends Simpleongesturelistener {
@Override
public boolean Ondoubletap (Motionevent e) {
Return Super.ondoubletap (e);
}
@Override
public boolean ondoubletapevent (Motionevent e) {
Return Super.ondoubletapevent (e);
}
@Override
public boolean Ondown (Motionevent e) {
Return Super.ondown (e);
}
@Override
public boolean onfling (Motionevent E1, motionevent E2, float x,
Float y) {
Return Super.onfling (E1, E2, X, y);
}
@Override
public void Onlongpress (Motionevent e) {
Super.onlongpress (e);
}
@Override
public boolean onscroll (Motionevent E1, motionevent E2, float x,
Float y) {
Return Super.onscroll (E1, E2, X, y);
}
@Override
public void Onshowpress (Motionevent e) {
Super.onshowpress (e);
}
@Override
public boolean onsingletapconfirmed (Motionevent e) {
Return super.onsingletapconfirmed (e);
}
@Override
public boolean onsingletapup (Motionevent e) {
Return Super.onsingletapup (e);
}
}
You can add specific processing methods to the function as needed, and then pass in the Gesturedetector through the constructor function.
Gesturedetector gesturedetector=new Gesturedetector (This,simpleongesturelistener);
Four. Another way of thinking
To achieve capture screen gestures, in addition to creating gesturedetector in the activity, there is a way of thinking: Build a overlay, this overlay implement Ongesturelistener interface, Make it maintain its own gesturedetector.
The ability to capture gestures is achieved by adding this overlay to the main view and passing in the corresponding listener.
Reprint, original address: http://blog.csdn.net/song_shi_chao/article/details/8224742
Android Gestures & Touch events