Android Gesture Slide (Left Slide and Right Slide)
- 2021-12-05 07:13:03
- OfStack
Recently, I want to realize the function of Android sliding left pop-up menu box and sliding right disappearing menu. Understand that the sliding event of Android under 1 must be implemented on view component or Activity, and two interfaces, OnTouchListener and OnGestureListener, must be implemented at the same time.
public class MyRelativeLayout extends RelativeLayout implements GestureDetector.OnGestureListener{
private float mPosX, mPosY, mCurPosX, mCurPosY;
private static final int FLING_MIN_DISTANCE = 20;// Moving minimum distance
private static final int FLING_MIN_VELOCITY = 200;// Maximum moving speed
// Building a gesture detector
GestureDetector mygesture = new GestureDetector(this);
public MyRelativeLayout(Context context){
super(context)
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
// TODO Auto-generated method stub
return mDetector.onTouchEvent(arg0);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
// e1 : No. 1 1 A ACTION_DOWN MotionEvent
// e2 : Finally 1 A ACTION_MOVE MotionEvent
// velocityX : X Moving speed on the axis (pixels) / Seconds)
// velocityY : Y Moving speed on the axis (pixels) / Seconds)
// X The coordinate displacement of the axis is greater than FLING_MIN_DISTANCE And the moving speed is greater than FLING_MIN_VELOCITY Pixels / Seconds
// To the left
if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE){
// && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
collapse();
}
// Up
if (e2.getY() - e1.getY() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
}
return false;
}
}
Add another paragraph to realize the gesture sliding effect:
Gesture sliding is actually a touch event
public class PhoneGuard01 extends Activity {
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_guard01);
// Creating Gesture Recognition Objects , And create a monitor for gesture recognition
mGestureDetector = new GestureDetector(this,new SimpleOnGestureListener(){
// This method needs to be overridden by itself
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
float X1=e1.getX();// Get the coordinate point pressed down ,X Shaft
float X2=e2.getX();// Get the lifted coordinate point ,Y Shaft
float Y1=e1.getY();// Get pressed Y Axial coordinate point
float Y2=e1.getY();// Get lifted Y Axial coordinate point
//Y The moving distance of, compared with X The moving distance of is large, so do not do any operation
if(Math.abs(Y1-Y2)>Math.abs(X1-X2)){
return false;
}
if(X1>X2){// Represents under 1 Page
nextPage(null);
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
/**
The following code means that , Touch events recognized by your gesture ,
Let the parent class call
*/
//onTouchEvent(MotionEvent event) Is inherited from View Object's
@Override
public boolean onTouchEvent(MotionEvent event) {
//mGestureDetector.onTouchEvent(event) Yes GestureDetector Your own
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
//----------------- The above is the code implementation of gesture recognition ------------------------------
// Jump to the next 1 Pages
public void nextPage(View v){
Intent intent=new Intent(this,PhoneGuard02.class);
startActivity(intent);
finish();
}
}