Android Touch and gesture Operation Gesturedetector

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/xyz_lmn/article/details/16826669

Now the smartphone dare not say hundred is touch screen, also should be more than 99% for touch screen, touch screen for our operation without keyboard, mouse-free mobile system brought a lot of convenience. When the user touches the screen, it produces a lot of touch events, down, up, move, and so on. The view class has a View.ontouchlistener internal interface that, by overriding his OnTouch(View V, motionevent event) method, We can handle some of the touch events as follows:

public class Mainactivity extends activity {...//This example shows a activity, but you would use the same approach if// You were subclassing a View. @Overridepublic boolean ontouchevent (Motionevent event) {int action = MOTIONEVENTCOMPAT.G    Etactionmasked (event);            Switch (action) {case (Motionevent.action_down): Log.d (Debug_tag, "action is Down");        return true;            Case (Motionevent.action_move): Log.d (Debug_tag, "ACTION is MOVE");        return true;            Case (MOTIONEVENT.ACTION_UP): Log.d (Debug_tag, "ACTION is up");        return true;            Case (Motionevent.action_cancel): Log.d (Debug_tag, "ACTION is CANCEL");        return true; Case (Motionevent.action_outside): Log.d (Debug_tag, "movement occurred OUTSIDE Bounds" + "O            F Current screen element ");              return true;    Default:return super.ontouchevent (event); }      }


The events provided by Ontouch are relatively simple, and if you need to deal with some complex gestures, it can be cumbersome to use this interface, because we have to judge what gestures are based on the user's touch trajectory. The Android SDK provides us with Gesturedetector (Gesture: Gesture Detector: recognition) classes, which we can identify a lot of gestures.

Public class Gesturedetector extends Object     

Java.lang.Object

Android.view.GestureDetector

Gesturedetector belongs to Android.view Pack, Android also provides android.gesture package support for more gesture operations, which we will introduce later. The official introduction uses the Gesturedetectorcompat to handle gesture recognition, why the use of Gesturedetectorcompat replaced the Gesturedetector, the official explanation:



There are two ways to instantiate Gesturedetectorcompat :


the Gesturedetector class provides two external interfaces: Ongesturelistener,ondoubletaplistener, and an internal class Simpleongesturelistener The Simpleongesturelistener class is a more convenient class for Gesturedetector to respond to different gestures, which implements the above two interfaces, which is a static class, meaning that it is actually an external class, We can inherit this class externally, overriding the gesture handling method inside. So there are two ways to implement gesture recognition, one to implement the Ongesturelistener interface, and the other to use the Simpleongesturelistener class.

Ongesturelistener has the following actions:

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.


Using the Ongesturelistener interface, you need to overload all the methods of the Ongesturelistener interface to listen for all gestures, as the official document mentions, "Detecing all supported Gestures ".

public class Mainactivity extends Activity implements Gesturedetector.ongesturelistener, Gesturedetector.on    doubletaplistener{private static final String Debug_tag = "gestures";     Private Gesturedetectorcompat Mdetector;     Called when the activity is first created.        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Instantiate the gesture detector with the//application context and an implementation of//Gesturedetec Tor.        Ongesturelistener mdetector = new Gesturedetectorcompat (this,this);        Set the gesture detector as the double tap//listener.    Mdetector.setondoubletaplistener (this);        } @Override public boolean ontouchevent (Motionevent event) {this.mDetector.onTouchEvent (event);    Be sure to call the superclass implementation return Super.ontouchevent (event); } @OverrIDE public boolean Ondown (Motionevent event) {LOG.D (Debug_tag, "Ondown:" + event.tostring ());    return true; } @Override public boolean onfling (motionevent event1, motionevent event2, float Velocityx, float veloci        TYy) {log.d (Debug_tag, "onfling:" + event1.tostring () +event2.tostring ());    return true; } @Override public void Onlongpress (Motionevent event) {LOG.D (Debug_tag, "onlongpress:" + event.tostring ())     ;        } @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {        LOG.D (Debug_tag, "onscroll:" + e1.tostring () +e2.tostring ());    return true; } @Override public void Onshowpress (Motionevent event) {LOG.D (Debug_tag, "onshowpress:" + event.tostring ())    ; } @Override public boolean onsingletapup (Motionevent event) {LOG.D (Debug_tag, "onsingletapup:" + event.tost        Ring ());    return true; } @Override PUBlic boolean Ondoubletap (Motionevent event) {LOG.D (Debug_tag, "Ondoubletap:" + event.tostring ());    return true; } @Override public boolean ondoubletapevent (Motionevent event) {LOG.D (Debug_tag, "ondoubletapevent:" + even        T.tostring ());    return true; } @Override public boolean onsingletapconfirmed (Motionevent event) {LOG.D (Debug_tag, "onsingletapconfirmed:        "+ event.tostring ());    return true; }}


This can cause some gesture actions that we can't use, but also overloaded. The advent of the Simpleongesturelistener class solves this problem if you want to "detecting a subset of supported gestures", Simpleongesturelistener is the best choice.

public class Mainactivity extends Activity {private Gesturedetectorcompat mdetector;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Mdetector = new Gesturedetectorcompat (This, new Mygesturelistener ());        } @Override public boolean ontouchevent (Motionevent event) {this.mDetector.onTouchEvent (event);    Return Super.ontouchevent (event); } class Mygesturelistener extends Gesturedetector.simpleongesturelistener {private static final String Debug_ta         G = "gestures";             @Override public boolean Ondown (Motionevent event) {LOG.D (Debug_tag, "Ondown:" + event.tostring ());        return true; } @Override public boolean onfling (motionevent event1, motionevent event2, float Velocityx, Float velocityy) {log.d (Debug_tag, "onfling:" + event1.tostring () +event2.tostring ());            return true; }    }}


Finally, we also explain two questions:

1, why use the Motioneventcompat in ontouchevent, but not directly use motionevent. Because Motioneventcompat makes more action available to API 4.

2, the Android view how to use gestures, the method is as follows:

View MyView = Findviewbyid (R.id.my_view);         Myview.setontouchlistener (New Ontouchlistener () {public            Boolean OnTouch (View V, motionevent event) {                //... Respond to touch Events                       this.mDetector.onTouchEvent (event);                Return Super.ontouchevent (event);            }        );




Sample Download

Reference:

Http://developer.android.com/reference/android/view/GestureDetector.html

Http://developer.android.com/training/gestures/detector.html


Android Touch and gesture Operation Gesturedetector

Related Article

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.