See examples directly:
Package Com.example.gesturedetector;import Android.os.bundle;import Android.app.activity;import Android.view.gesturedetector;import Android.view.gesturedetector.ongesturelistener;import Android.view.Menu; Import Android.view.motionevent;import Android.widget.toast;public class Mainactivity extends Activity implements Ongesturelistener {private gesturedetector mgesturedetector; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mGestureDetector = new Gesturedetector (this);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubmgesturedetector.ontouchevent (event); return Super.ontouchevent (event);} @Overridepublic boolean Ondown (motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, "Ondown", Toast.length_short). Show (); return false;} @Overridepublic boolean onfling (motionevent startevent, motionevent endevent,float Velocityx, float velocityy) {//TODO auto-generated Method Stubif (Startevent.gety ()-endevent.gety () >) {toast.maketext (this, "gesture swipe up", toast.le Ngth_short). Show (); return true;} else if (startevent.gety ()-endevent.gety () < -100) {Toast.maketext (this, "swipe down", Toast.length_short). Show (); return true;} else if (Startevent.getx ()-endevent.getx () >) {toast.maketext (this, "gesture swipe left", Toast.length_short). Show (); return true;} else if (Startevent.getx ()-Endevent.getx () < -100) {Toast.maketext (this, "gesture right swipe", Toast.length_short). Show (); return true;} return false;} @Overridepublic void Onlongpress (Motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, " Onlongpress ", Toast.length_short). Show ();} @Overridepublic boolean onscroll (motionevent arg0, motionevent arg1, float arg2,float arg3) {//TODO auto-generated method Stub//toast.maketext (This, "onscroll", Toast.length_short). Show (); return false;} @Overridepublic void Onshowpress (Motionevent arg0) {//TODO auto-generated method Stub//toaSt.maketext (This, "onshowpress", Toast.length_short). Show (); @Overridepublic boolean onsingletapup (motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, " Onsingletapup ", Toast.length_short). Show (); return false;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}
Event Explanation:
1. Ondown (motionevent e): down event; 2. Onsingletapup (motionevent E): one click Up event; 3. Onshowpress (motionevent E): When the Down event occurs and move or up does not occur before the event is triggered; 4. Onlongpress (motionevent E): Long press event; 5. Onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy): swipe gesture event; 6. Onscroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag events on the screen.
Android uses Gesturedetector to achieve gesture swipe effects