Recognition of Android gesture swipe

Source: Internet
Author: User

Summary:

For gesture recognition in Android, you can start with the following three listener--ontouchlistener, Ongesturelistener, Ondoubletaplistener. These three listeners are touch listening, gesture swipe monitoring and screen double-tap operation monitoring respectively. A lot of the time we need these gesture recognition operations, such as our custom controls, are often used. These three listeners are described separately below.


Touch Listener Ontouchlistener

Let our activity go to the reality of this interface and override the Ontouch method. Overriding the Ontouch method of Ontouchlistener This method is called when the touch screen is touched, that is, when a touch event occurs (touching and stroking two events). The sample code is as follows:

@Override Public    Boolean OnTouch (View V, motionevent event) {        detector.ontouchevent (event);        Toast.maketext (This, "OnTouch", Time_out). Show ();        return true;    }

gesture Swipe listener Ongesturelistener

Let our activity go to the reality of this interface, and rewrite onfling, Onlongpress, Onscroll, Ondown, Onshowpress, Onsingletapup methods. The sample code is as follows:

/** * Do not call */@Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx) when gesture swipe, Float velocityy) {if (E1.getx ()-E2.getx () > Fling_min_distance) {toast.maketext (this, "swipe left"        , Time_out). Show ();        } else if (E2.getx ()-E1.getx () > Fling_min_distance) {toast.maketext (this, "swipe right", time_out). Show ();    } return false; }/** * Long time is called */@Override public void onlongpress (Motionevent e) {Toast.maketext (this, "trigger long Press callback"    , Time_out). Show ();            /** * * When scrolling * * * @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,        Float Distancey) {Toast.maketext (this, "trigger scrolling callback", Time_out). Show ();    return false;  /** * is called when the action is pressed */@Override public boolean ondown (Motionevent e) {Toast.maketext (this, "Press callback",        Time_out). Show ();    return false; }/** * is called when pressed */@Override    public void Onshowpress (Motionevent e) {Toast.maketext (this, "Press and hold the callback", Time_out). Show (); }/** * is called when lifted */@Override public boolean onsingletapup (Motionevent e) {Toast.maketext (this, "trigger        Lift the callback ", Time_out). Show ();    return false; }

Double-click Screen Listener Ondoubletaplistener

Let's get our activity to reality this interface and rewrite ondoubletap, Ondoubletapevent, Onsingletapconfirmed method. The sample code is as follows:

@Override Public    Boolean Ondoubletap (Motionevent arg0) {        Toast.maketext (this, "trigger double-click callback", Time_out). Show ();        return false;    }    @Override Public    Boolean ondoubletapevent (Motionevent arg0) {        Toast.maketext (this, "triggering the double-click Press and Lift Callback", time_out) . Show ();        return false;    }    @Override Public    Boolean onsingletapconfirmed (Motionevent arg0) {        Toast.maketext (this, "trigger Click Acknowledgement Callback", Time_ Out). Show ();        return false;    }


Recognition of Android gesture swipe

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.