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.
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.
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);
- }
- });
- }
- }
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
- click: down 0, single up 1, single conf 0
- short press: down 0, show 0, single up 1
- long press: down 0, show 0, long 0
-
- scroll: down 0, (show 0), scrool 2...
- swipe: down 0, (show 0), scrool 2..., fling 1
-
Android gesture recognition (click Double tap to lift short press long to scroll)