The example of this article describes the Android gesture operation method. Share to everyone for your reference, specific as follows:
Android provides an interface for judging gestures, so we can implement a variety of gesture functions based on the API provided to improve the user experience of mobile apps.
Here is a small demo I wrote:
Gestureactivity.java
public class Gestureactivity extends activity {private Gesturedetector gesturedetector;
private screen screens;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Gesturedetector = new Gesturedetector (This,ongesturelistener);
Get the size of screen = Gestureutils.getscreenpix (this);
@Override public boolean ontouchevent (Motionevent event) {return gesturedetector.ontouchevent (event);
} gesturedetector.ongesturelistener Ongesturelistener = new Gesturedetector.simpleongesturelistener () {@Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {float x = E2.get
X ()-E1.getx ();
Float y = e2.gety ()-e1.gety ();
The limit must be 1/3 across the screen to calculate the float x_limit = SCREEN.WIDTHPIXELS/3;
float y_limit = SCREEN.HEIGHTPIXELS/3;
float X_abs = Math.Abs (x);
float y_abs = Math.Abs (y); if (x_abs >= y_abs) {//gesture left or right if (x > X_limit | | | x <-X_LIMIT) {if (x>0) {
Right show (' right ');
}else if (x//left Show ("left");
}}else{//gesture down or up if (Y > Y_limit | | | y <-y_limit) {if (y>0) {
Down Show ("Down");
}else if (Y//up show ("Up");
}} return true;
}
};
private void Show (String value) {Toast.maketext (this, value, Toast.length_short). Show ();
}
}
Gestureutils.java
public class Gestureutils {
//Get screen size public
static Screens Getscreenpix (context context) {
Displaymetrics DM = new Displaymetrics ();
WindowManager WindowManager = (windowmanager) context.getsystemservice (context.window_service);
Windowmanager.getdefaultdisplay (). Getmetrics (DM);
Return to New screen (dm.widthpixels,dm.heightpixels);
}
public static class screen{public
int widthpixels;
public int heightpixels;
Public screens () {
} public screen
(int widthpixels,int heightpixels) {
this.widthpixels=widthpixels;
this.heightpixels=heightpixels;
}
@Override public
String toString () {return
"(" +widthpixels+ "," +heightpixels+ ")" "
;
}
}}
For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with your Android programming.