Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.GestureDetector;
Import Android.view.GestureDetector.OnGestureListener;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnTouchListener;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Gestureactivity extends Activity implements Ontouchlistener,
Ongesturelistener {
Gesturedetector detector;
Public gestureactivity () {
detector = new Gesturedetector (this);
}
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
TextView TV = (TextView) Findviewbyid (r.id.textview001);
Set up a TV listener
Tv.setontouchlistener (this);
Tv.setfocusable (TRUE);
The view is required to handle the hold that differs from tap (touch)
Tv.setclickable (TRUE);
Tv.setlongclickable (TRUE);
Detector.setislongpressenabled (TRUE);
}
public boolean OnTouch (View V, motionevent event) {
Return Detector.ontouchevent (event);
}
User touches touch screen, triggered by 1 motionevent action_down
public boolean Ondown (Motionevent arg0) {
LOG.I ("Mygesture", "Ondown");
Toast.maketext (This, "Ondown", Toast.length_short). Show ();
return true;
}
public void Onshowpress (Motionevent e) {
LOG.I ("Mygesture", "onshowpress");
Toast.maketext (This, "onshowpress", Toast.length_short). Show ();
}
User (after touch touchscreen) released, triggered by a 1 motionevent action_up
public boolean onsingletapup (Motionevent e) {
LOG.I ("Mygesture", "Onsingletapup");
Toast.maketext (This, "Onsingletapup", Toast.length_short). Show ();
return true;
}
The user presses the touchscreen, moves quickly, and releases, triggered by 1 motionevent action_down, multiple action_move, and 1 action_up
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {
LOG.I ("Mygesture", "onfling");
Parameter explanation:
E1:1th Action_down motionevent
E2: The Last Action_move motionevent
Movement speed on velocityx:x axis, pixels per second
Movement speed on velocityy:y axis, pixels per second
Trigger conditions:
X-axis coordinate displacement greater than fling_min_distance, and moving faster than fling_min_velocity pixels per second
Final int fling_min_distance = +, fling_min_velocity = 200;
if (E1.getx ()-E2.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {
Fling Left
LOG.I ("Mygesture", "Fling left");
Toast.maketext (This, "Fling left", Toast.length_short). Show ();
} else if (E2.getx ()-E1.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {
Fling Right
LOG.I ("Mygesture", "Fling right");
Toast.maketext (This, "Fling right", Toast.length_short). Show ();
} else if (E2.gety ()-e1.gety () >fling_min_distance && math.abs (velocityy) >fling_min_velocity) {
Fling down
LOG.I ("Mygesture", "Fling down");
Toast.maketext (This, "Fling", Toast.length_short). Show ();
} else if (E1.gety ()-e2.gety () >fling_min_distance && math.abs (velocityy) >fling_min_velocity) {
Fling up
LOG.I ("Mygesture", "Fling up");
Toast.maketext (This, "Fling up", Toast.length_short). Show ();
}
return false;
}
The user presses the touchscreen, and drags, by 1 motionevent action_down, multiple Action_move trigger
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {
LOG.I ("Mygesture", "onscroll");
Toast.maketext (This, "onscroll", Toast.length_long). Show ();
return true;
}
User long press touch screen, triggered by multiple motionevent Action_down
public void Onlongpress (Motionevent e) {
LOG.I ("Mygesture", "onlongpress");
Toast.maketext (This, "onlongpress", Toast.length_long). Show ();
}
}
Android Gesturedetector gesture judgment