[Android] mobile guard swipe to switch the screen, android gesture
Define a gesture identification device
Get the GestureDetector object of the gesture identification device, and use the new GestureDetector (context, listener), parameter: context, listener
The anonymous internal class implements the simple gesture listener SimpleOnGestureListener interface and overrides the onFling () sliding method.
Four parameters are passed in:
MotionEvent e1, MotionEvent e2, velocityX, velocityY
E1 is the first vertex, e2 is the second vertex, X axis speed, Y axis speed
When the first vertex minus the second vertex is greater than 200, we think it is to draw from right to left, next page
When the second vertex minus the first vertex is greater than 200, we think it is to draw from left to right.
Call the getRawX () of the MotionEvent object to obtain the coordinates of the X axis.
Use a gesture identification tool to recognize gestures
Override the onTouchEvent () method of the activity to obtain the sliding event of the gesture on the interface.
A MotionEvent object is passed in.
Call the onTouchEvent (event) method of the GestureDetector object. The parameter is the MotionEvent object and the obtained event is passed in.
Shield oblique strokes
When the distance between the Y axis coordinates of two points is greater than 100, we think it is oblique.
Call the getRawY () of the MotionEvent object to obtain the Y axis coordinate. The difference between the two points is the absolute value Math. abs (). If the value is greater than 100, true is returned. Do not proceed.
If the SimpleOnGestureListener class is not found, use new GestureDetector. SimpleOnGestureListener ()
Extract common methods to base class abstract class BaseSecActivity. Your activity only needs to inherit this base class and implement the abstract methods on the top and bottom pages to achieve the sliding effect between left and right.
BaseSecGuideActivity. java
Package com. qingguow. mobilesafe; import android. app. activity; import android. OS. bundle; import android. view. gestureDetector; import android. view. motionEvent; public abstract class BaseSecGuideActivity extends Activity {// define the gesture reader protected GestureDetector role; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); // instantiate gestureDetector = new GestureDetector (this, new GestureDetector. simpleOnGestureListener () {@ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// shield if (Math. abs (e1.getRawY ()-e2.getRawY ()> 100) {return true;} if (e1.getRawX ()-e2.getRawX ()> 100) {System. out. println ("right to left, next page"); showNext (); return true;} if (e2.getRawX ()-e1.getRawX ()> 100) {System. out. println ("from left to right, previous page"); showPre (); return true;} return super. onFling (e1, e2, velocityX, velocityY) ;}});} public abstract void showPre (); @ Override public boolean onTouchEvent (MotionEvent event) {gestureDetector. onTouchEvent (event); return super. onTouchEvent (event);} public abstract void showNext ();}