Gestureutils.java
PackageCom.gesture;
ImportAndroid.content.Context;
ImportAndroid.util.DisplayMetrics;
ImportAndroid.view.WindowManager;
Public Classgestureutils {
//Get the size of the screen
Public StaticScreen Getscreenpix (context context) {
Displaymetrics DM= NewDisplaymetrics ();
WindowManager WindowManager=(WindowManager) Context.getsystemservice (Context.window_service);
Windowmanager.getdefaultdisplay (). Getmetrics (DM);
Return NewScreen (dm.widthpixels,dm.heightpixels);
}
Public Static Classscreen{
Public IntWidthpixels;
Public IntHeightpixels;
PublicScreen () {
}
PublicScreen (IntWidthpixels,IntHeightpixels) {
This. widthpixels=Widthpixels;
This. heightpixels=heightpixels;
}
@Override
public String toString () {
Span style= "color: #0000ff;" >return ( "+widthpixels+ "," +heightpixels+) Span style= "color: #000000;" > ";
}
}
} /span>
Builegestureext.java
PackageCom.gesture;
ImportCom.gesture.GestureUtils.Screen;
ImportAndroid.content.Context;
ImportAndroid.view.GestureDetector;
ImportAndroid.view.MotionEvent;
Public ClassBuilegestureext {
Public Static Final IntGesture_up=0;
Public Static Final IntGesture_down=1;
Public Static Final IntGesture_left=2;
Public Static Final IntGesture_right=3;
PrivateOngestureresult Ongestureresult;
PrivateContext Mcontext;
PublicBuilegestureext (Context c,ongestureresult ongestureresult) {
This. mcontext=C
This. ongestureresult=Ongestureresult;
Screen=Gestureutils.getscreenpix (c);
}
PublicGesturedetector Buile ()
{
Return NewGesturedetector (Mcontext, Ongesturelistener);
}
PrivateScreen screen;
PrivateGesturedetector.ongesturelistener Ongesturelistener= NewGesturedetector.simpleongesturelistener () {
@Override
Public BooleanOnfling (motionevent E1, motionevent E2,FloatVelocityx,
FloatVELOCITYY) {
FloatX=E2.getx ()-E1.getx ();
FloatY=E2.gety ()-E1.gety ();
//The limit must be 1/4 across the screen to be counted.
FloatX_limit=Screen.widthpixels/ 4;
FloatY_limit=Screen.heightpixels/ 4;
FloatX_abs=Math.Abs (x);
FloatY_abs=Math.Abs (y);
If(X_abs>=Y_abs) {
//Gesture left or right
If(x>X_limit||X< -X_limit) {
If(x>0){
//Right
Doresult (Gesture_right);
}Else If(x<=0){
//Left
Doresult (Gesture_left);
}
}
}Else{
//Gesture down or up
If (y > Y_limit || y < -y_limit) {
if (y< Span style= "color: #000000;" >>0 //down Doresult (Gesture_down);} else if (y<=0) {//up doresult (gesture_up);}} } return true; } }; public void Doresult (int result) {if (ongestureresult!=null) {Ongestureresult.ongestureresult (Result),}} public Interf Ace Ongestureresult {public void ongestureresult (int direction);}
Demo
package
com.gesture;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.GestureDetector;
import
android.view.MotionEvent;
import
android.widget.Toast;
public
class
GesturetestActivity
extends
Activity {
/** Called when the activity is first created. */
private
GestureDetector gestureDetector;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector =
new
BuileGestureExt(
this
,
new
BuileGestureExt.OnGestureResult() {
@Override
public
void
onGestureResult(
int
direction) {
show(Integer.toString(direction));
}
}
).Buile();
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
return
gestureDetector.onTouchEvent(event);
}
private
void
show(String value){
Toast.makeText(
this
, value, Toast.LENGTH_SHORT).show();
}
}