Understanding Android's gesture recognition improves app's user experience _android

Source: Internet
Author: User
Tags touch
For the touch screen, its original message is nothing more than press, lift, move these several, we only 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 very helpful.

Base
Gesturedetector's working principle is that when we receive the user to touch the message, the message is given to Gesturedetector to process, and we get the gesture of gesturedetector processing by setting the listener.

Gesturedetector provides two listener interfaces, Ongesturelistener handles click class messages, Ondoubletaplistener handles double-clicking class messages.

The Ongesturelistener interface has these several:
Click to trigger immediately when the touch screen is pressed
Abstract Boolean Ondown (Motionevent e);
Lift, when the finger left the touch screen triggered (long press, scrolling, sliding, does not trigger this gesture)
Abstract Boolean onsingletapup (Motionevent e);
Short press, the touch screen pressed after a moment to lift, will trigger this gesture, if the rapid lift will not
abstract void Onshowpress (Motionevent e);
Long press, touch screen after press neither lift nor move, after a period of time triggered
abstract void Onlongpress (Motionevent e);
Scrolling, the touch screen is pressed and then moved
Abstract Boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey);
Slide, the touch screen is pressed and quickly moved and lifted, it triggers a scrolling gesture and then triggers a sliding gesture
Abstract Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy);
The Ondoubletaplistener interface has these several:
Double-click, finger on the touch screen quickly hit the second trigger
Abstract Boolean Ondoubletap (Motionevent e);
Double-click Press and lift each trigger once
Abstract Boolean ondoubletapevent (Motionevent e);
Click Confirm, ie quickly press and lift, but not continuously click Second
Abstract Boolean onsingletapconfirmed (Motionevent e);
Sometimes we don't have to deal with all of the above gestures, and for convenience, Android offers another class Simpleongesturelistener that implements the above interface, We just need to inherit Simpleongesturelistener and then overload the gesture of interest.

Application
Step 1: Create a Gesture Listener object
Copy Code code as follows:

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 gesture recognition in the activity:
Copy Code code as follows:

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:
Copy Code code as follows:

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);
Setlongclickable (TRUE);
This.setontouchlistener (New Ontouchlistener () {
public boolean Ontouch (View V, motionevent event) {
Return Mgesturedetector.ontouchevent (event);
}
});
}
}

Traps
For custom view, using gesture recognition for two traps can be a waste of your time.
1:view must set Longclickable to true, or 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 ontouchevent as an activity, otherwise the same gesture recognition will not work correctly

Test Results
The following is a sequence of gestures returned by a variety of operations, with a value of 0 indicating that the touch screen is pressed and 1 indicates lift
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
Double-click: down 0, single up 1, double 0, double event 0, down 0, double event 1
Scrolling: down 0, (show 0), Scrool 2 ...
Sliding: down 0, (show 0), Scrool 2 ..., fling 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.