Deep understanding of Android gesture Recognition _android

Source: Internet
Author: User

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.

Basis

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 the
abstract Boolean ondown (Motionevent e) immediately when the touchscreen presses;
Lift, when the finger leaves the touch screen triggered (long press, scrolling, sliding, does not trigger this gesture)
abstract Boolean onsingletapup (Motionevent e);
Short press, the touch screen press 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 pressing neither lift nor move, after a period of time to trigger the
abstract void onlongpress (Motionevent e);
Scrolling, touch screen press after moving
abstract boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey);
Sliding, the touch screen is pressed and quickly moved and lifted, will trigger the scrolling gesture, then trigger a sliding gesture
abstract Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy);
Ondoubletaplistener interface has these several:
//Double click, the finger on the touch screen quickly click on the second trigger
abstract Boolean Ondoubletap (Motionevent e);
Double-click the press and lift each trigger an
abstract Boolean ondoubletapevent (Motionevent e);
Click OK, ie quickly press and lift, but not continuously click on the 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 gesture listener objects

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 {mcontext = context; @Override public boolean Ondown (Motionevent e) {toast.maketext (Mcontext, ' down ' + e.getaction (), Toast.length_shor
  T). Show ();
 return false; @Override public void onshowpress (Motionevent e) {toast.maketext (Mcontext, "show" + e.getaction (), Toast.length_sh   
 ORT). 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.maket
  Ext (Mcontext, "SCROLL" + e2.getaction (), Toast.length_short). Show (); return False; @Override public void onlongpress (Motionevent e) {toast.maketext (Mcontext, "LONG" + e.getaction (), Toast.length_sh
 ORT). Show (); @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {Toast.makete
  XT (Mcontext, "Fling" + e2.getaction (), Toast.length_short). Show ();
 return false; @Override public boolean Ondoubletap (Motionevent e) {toast.maketext (Mcontext, DOUBLE + e.getaction (), Toast.leng
  Th_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.getact

  Ion (), Toast.length_short). Show ();

 return false; 
 }

}

Step 2: set gesture recognition

We can set gesture recognition in the 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, 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);}}

  );

 }

 

Trap

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.