The Android SDK provides us with the Gesturedetector class, which allows us to identify a lot of gestures, mainly through his ontouchevent method, to complete the identification of different gestures.
Gesturedetector This class provides two interfaces and an internal class.
Interface: Ongesturelistener,ondoubletaplistener
Inner class: Simpleongesturelistener
The Simpleongesturelistener method is as follows:
Boolean onsingletapup (Motionevent e)
void Onlongpress (Motionevent e)
Boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey)
Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy)
void Onshowpress (Motionevent e)
Boolean Ondown (Motionevent e)
Boolean Ondoubletap (Motionevent e)
Boolean ondoubletapevent (Motionevent e)
Boolean onsingletapconfirmed (Motionevent e)
The following code implements left-and right-slip applications:
1 PackageCom.cskaoyan.mobilemanager;2 3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.GestureDetector;7 Importandroid.view.MotionEvent;8 ImportAndroid.view.View;9 ImportAndroid.view.GestureDetector.SimpleOnGestureListener;Ten One Public Abstract classSetupbaseactivityextendsActivity { A Privategesturedetector Gesturedetector; - - @Override the protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub - Super. OnCreate (savedinstancestate); -Gesturedetector =NewGesturedetector ( This,NewMygesturedetectorlinstener ()); + - } + A classMygesturedetectorlinstenerextendssimpleongesturelistener{ at @Override - Public Booleanonfling ( -Motionevent E1,//The point that starts to slide -Motionevent E2,//The point where the slide stops - floatVelocityx,//the speed of sliding in the x direction - floatVELOCITYY)//the speed of sliding in the Y direction in { - //TODO auto-generated Method Stub to floatStartX =e1.getx (); + floatStarty =e1.gety (); - floatEndX =e2.getx (); the floatEndy =e2.gety (); * $ if(Endx-startx >150 ) {Panax Notoginseng //swipe to previous page - previous (); the System.out +. println ("Setup1Activity.MyGestureDetectorLinstener.onFling () show Previous" ); A } the Else if(startx-endx>150) { + System.out -. println ("Setup1Activity.MyGestureDetectorLinstener.onFling () Show Next" ); $ //swipe to the next page $ next (); - } - return Super. Onfling (E1, E2, Velocityx, velocityy); the } - Wuyi the } - Wu @Override - Public Booleanontouchevent (Motionevent event) { About //TODO auto-generated Method Stub $ - gesturedetector.ontouchevent (event); - return Super. Ontouchevent (event); - } A + Public Abstract voidnext (); the Public Abstract voidprevious (); -}
Then the abstraction of two methods next () and previous (), the need to jump through fling activity can inherit setupbaseactivity and implement both methods is OK.
Introduction to 025_01gesturedetector class and its usage