A simple gesture function is used to do a project (left, right, slide the screen), so it simply encapsulates the
Import Android.content.Context;
Import Android.util.DisplayMetrics;
Import Android.view.GestureDetector;
Import Android.view.GestureDetector.OnGestureListener;
Import android.view.MotionEvent;
public class Gesturehelper implements Ongesturelistener {
Private Gesturedetector Gesture_detector;
private int screen_width;
Private Onflinglistener listener_onfling;
public static abstract class Onflinglistener {
public abstract void Onflingleft ();
public abstract void Onflingright ();
}
Public Gesturehelper {
Displaymetrics DM = context.getresources (). Getdisplaymetrics ();
Screen_width = Dm.widthpixels;
Gesture_detector = new Gesturedetector (context, this);
}
public void Setonflinglistener (Onflinglistener listener) {
listener_onfling = listener;
}
public boolean ontouchevent (Motionevent event) {
Return Gesture_detector.ontouchevent (event);
}
@Override
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {
Trigger Condition:
The x-coordinate of the x-axis is greater than the fling_min_distance and the moving speed is greater than fling_min_velocity pixels per second
Final int fling_min_distance = (int) (screen_width/3.0f), fling_min_velocity = 200;
if (E1.getx ()-E2.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {
Listener_onfling. Onflingleft ();
else if (E2.getx ()-E1.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {
Listener_onfling. Onflingright ();
}
return true;
}
@Override
public boolean Ondown (Motionevent e) {
return false;
}
@Override
public void Onlongpress (Motionevent e) {
}
@Override
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,
Float Distancey) {
return false;
}
@Override
public void Onshowpress (Motionevent e) {
}
@Override
public boolean onsingletapup (Motionevent e) {
return false;
}
}
Use the following methods:
public class Testactivity extends activity {
Private Gesturehelper GH;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_start);
GH = new Gesturehelper (this);
Gh.setonflinglistener (New Onflinglistener () {
@Override
public void Onflingleft () {
Slide Left
}
@Override
public void Onflingright () {
Slide Right
}
});
}
@Override
public boolean ontouchevent (Motionevent event) {
Return Gh.ontouchevent (event);
}
}