Effect: one finger to achieve (all gesture events) and (part of the event);
A. All gestures
Activity_main.xml
<textview
android:id= "@+id/gesture"
android:layout_width= "Match_parent"
150DP "
android:gravity=" center "
android:text=" click gesture Change "/><textview
android:id=" @+id/doubletap "
android:layout_width= "match_parent"
android:layout_height= "150DP"
android:gravity= "center"
Mainactivity.java
public class Mainactivity extends activity {private static final String Log_tag = "Hellogesture";
Private Gesturedetector mgesturedetector = null;
Private TextView Mgesturetextview = null;
Private TextView Mdoubletaptextview = null;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mgesturetextview = (TextView) Findviewbyid (r.id.gesture);
Mdoubletaptextview = (TextView) Findviewbyid (R.ID.DOUBLETAP);
Constructs the Gesturedetector object, passing in the Listener object mgesturedetector = new Gesturedetector (this, mongesturelistener);
Incoming double-click Listener Object Mgesturedetector.setondoubletaplistener (Mdoubletaplistener); @Override public boolean ontouchevent (Motionevent event) {//passes the event to the gesture detection object in the Ontouchevent method, otherwise the callback function in the gesture listener object is not invoked mGe
Sturedetector.ontouchevent (event);
Return Super.ontouchevent (event); Private Ongesturelistener Mongesturelistener = new Ongesturelistener () {@Override public boolean onsingletapup (motio NEveNT e) {log.i (Log_tag, "onsingletapup:" + e.tostring ());
Mgesturetextview.settext ("Onsingletapup:");
return false;
@Override public void onshowpress (Motionevent e) {log.i (Log_tag, "onshowpress:" + e.tostring ());
Mgesturetextview.settext ("onshowpress:"); @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {log.i (Log_tag,
"Onscroll:" + e1.tostring () + "," + e2.tostring ());
Mgesturetextview.settext ("Onscroll");
return false;
@Override public void onlongpress (Motionevent e) {log.i (Log_tag, "onlongpress:" + e.tostring ());
Mgesturetextview.settext ("onlongpress:"); @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {log.i (Log_tag,
"Onfling:" + e1.tostring () + "," + e2.tostring ());
Mgesturetextview.settext ("onfling");
return false;
@Override public boolean Ondown (Motionevent e) {log.i (Log_tag, "Ondown:" + e.tostring ()); Mgesturetextview.SetText ("Ondown:");
return false; }
}; Private Ondoubletaplistener Mdoubletaplistener = new Ondoubletaplistener () {@Override public boolean Onsingletapconfir
Med (motionevent e) {log.i ("Log_tag", "onsingletapconfirmed:" + e.tostring ());
Mdoubletaptextview.settext ("onsingletapconfirmed:");
return false;
@Override public boolean ondoubletapevent (Motionevent e) {log.i ("Log_tag", "ondoubletapevent:" + e.tostring ());
Mdoubletaptextview.settext ("ondoubletapevent:");
return false;
@Override public boolean Ondoubletap (Motionevent e) {log.i ("Log_tag", "Ondoubletap:" + e.tostring ());
Mdoubletaptextview.settext ("Ondoubletap:");
return false; }
};}
B. Partial gestures
If you only want to handle a few gestures, you can choose to inherit the Gesturedetector.simpleongesturelistener class instead of implementing
Gesturedetector.ongesturelistener interface
Mainactivity.java
public class Mainactivity extends activity {private Gesturedetector mgesturedetector = null;
Private TextView Mgesturetextview = null;
Private TextView Mdoubletaptextview = null;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mgesturetextview = (TextView) Findviewbyid (r.id.gesture);
Mdoubletaptextview = (TextView) Findviewbyid (R.ID.DOUBLETAP);
Constructs the Gesturedetector object, passing in the Listener object mgesturedetector = new Gesturedetector (This, new Mygesturelistener ()); Incoming double-click Listener Object} @Override public boolean ontouchevent (Motionevent event) {//Pass the event to the gesture detection object in the Ontouchevent method, otherwise the gesture listens to the back in the object
The function is This.mGestureDetector.onTouchEvent (event) that will not be invoked;
Return Super.ontouchevent (event); Class Mygesturelistener extends Gesturedetector.simpleongesturelistener {private static final String Debug_tag = "Ge
Stures "; @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {LOG.D (Debug_tag, "onfling:" + e1.tostring () + "," + e2.tostring ());
Mgesturetextview.settext ("onfling");
return false;
@Override public boolean Ondown (Motionevent e) {log.d (Debug_tag, "Ondown:" + e.tostring ());
Mgesturetextview.settext ("Ondown:");
return false; }
}}