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