For the touchscreen, its native message simply presses, lifts, and moves these kinds of things, we just need to simply overload Ontouch or set the touch listener Setontouchlistener can be processed. However, in order to improve the user experience of our app, sometimes we need to identify the user's gestures, and Android gives us the gesture recognition tool gesturedetector can be a big help.
Basis
Gesturedetector works by giving the message to Gesturedetector when we receive the user's touch message, and we get the gesturedetector-processed gesture by setting the listener.
Gesturedetector provides two listener interfaces, Ongesturelistener handles click class messages, and Ondoubletaplistener handles double-click class messages.
The Ongesturelistener interface has these several:
Click to trigger the touch screen as soon as it is pressed
Abstract Boolean Ondown (Motionevent e);
Lift, trigger when your finger leaves the touch screen (long press, scroll, swipe, does not trigger this gesture)
Abstract Boolean onsingletapup (Motionevent e);
Short press, touch screen pressed after a moment to lift, will trigger this gesture, if the rapid lifting will not
abstract void Onshowpress (Motionevent e);
Press and hold, touch the screen after press neither lift nor move, after a period of time triggered
abstract void Onlongpress (Motionevent e);
Scroll, touch screen to move after pressing
Abstract Boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey);
Swipe, the touch screen presses and moves quickly and lifts, triggering a scrolling gesture, followed by a swipe gesture
Abstract Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy);
The Ondoubletaplistener interface has these several:
Double click, the finger on the touch screen quickly click on the second when the trigger
Abstract Boolean Ondoubletap (Motionevent e);
Double-click to press and lift each trigger once
Abstract Boolean ondoubletapevent (Motionevent e);
Click Confirm, which is quickly pressed and lifted, but not consecutively click Second
Abstract Boolean onsingletapconfirmed (Motionevent e);
Sometimes we don't need to deal with all of the above gestures, for convenience, Android provides another class Simpleongesturelistener implements the above interface, We just need to inherit the Simpleongesturelistener and reload the gesture of interest.
Application
STEP 1: Create a Gesture Listener object
Package noodies.blog.csdn.net;
Import Android.content.Context;
Import android.view.MotionEvent;
Import Android.view.GestureDetector.SimpleOnGestureListener;
Import Android.widget.Toast;
public class Mygesturelistener extends Simpleongesturelistener {
Private Context Mcontext;
Mygesturelistener (Context context) {
Mcontext = context;
}
@Override
public boolean Ondown (Motionevent e) {
Toast.maketext (Mcontext, "Down" + e.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public void Onshowpress (Motionevent e) {
Toast.maketext (Mcontext, "show" + e.getaction (), Toast.length_short). Show ();
}
@Override
public boolean onsingletapup (Motionevent e) {
Toast.maketext (Mcontext, "single Up" + E.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public boolean onscroll (Motionevent E1, motionevent E2,
Float Distancex, float distancey) {
Toast.maketext (Mcontext, "SCROLL" + e2.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public void Onlongpress (Motionevent e) {
Toast.maketext (Mcontext, "LONG" + e.getaction (), Toast.length_short). Show ();
}
@Override
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
Float velocityy) {
Toast.maketext (Mcontext, "FLING" + e2.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public boolean Ondoubletap (Motionevent e) {
Toast.maketext (Mcontext, "DOUBLE" + e.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public boolean ondoubletapevent (Motionevent e) {
Toast.maketext (Mcontext, "DOUBLE EVENT" + e.getaction (), Toast.length_short). Show ();
return false;
}
@Override
public boolean onsingletapconfirmed (Motionevent e) {
Toast.maketext (Mcontext, "single CONF" + E.getaction (), Toast.length_short). Show ();
return false;
}
}
STEP 2: Set gesture recognition
We can set up gesture recognition in activity:
Package noodies.blog.csdn.net;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
public class Gesturetestactivity extends Activity {
Private Gesturedetector Mgesturedetector;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mgesturedetector = new Gesturedetector (This, new Mygesturelistener (this));
}
@Override
public boolean ontouchevent (Motionevent event) {
Return Mgesturedetector.ontouchevent (event);
}
}
You can also set gesture recognition in a custom view:
Package noodies.blog.csdn.net;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
public class MyView extends View {
Private Gesturedetector Mgesturedetector;
Public MyView (context context, AttributeSet Attrs) {
Super (context, attrs);
Mgesturedetector = new Gesturedetector (context, new Mygesturelistener (context));
Setlongclickable (TRUE);
This.setontouchlistener (New Ontouchlistener () {
public boolean OnTouch (View V, motionevent event) {
Return Mgesturedetector.ontouchevent (event);
}
});
}
}
Trap
For a custom view, using gesture recognition with two traps can be a waste of your time.
1:view must set Longclickable to true, otherwise gesture recognition will not work correctly, only return down, Show, long three gestures
2: Gesture recognition must be invoked in the view's Ontouchlistener, not overloaded ontouchevent like activity, or the same gesture recognition will not work correctly
Test results
The following is a sequence of gestures returned by various operations, with a value of 0 indicating that the touch screen is pressed and 1 means lifting
Click: down 0, single to 1, single conf 0
Short press: down 0, show 0, single up 1
Long press: down 0, show 0, long 0
Double-click: down 0, single to 1, double 0, double event 0, down 0, double event 1
Scrolling: down 0, (show 0), Scrool 2 ...
Swipe: down 0, (show 0), Scrool 2 ..., fling 1
Excerpt from Noodies's column