Encapsulated android listens for finger swiping left and right on the screen for event class sharing
- 2020-06-19 11:45:08
- OfStack
Left and right sliding is the most commonly used smart phone action, in this simple package 1, after the direct use of it.
It simply takes a few lines, but the following class is encapsulated.
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// Here, xml is 1 A blank layout
// Need to listen for left-right sliding events view
RelativeLayout view = (RelativeLayout) this.findViewById(R.id.layout);
//setLongClickable Is a must
view.setLongClickable(true);
view.setOnTouchListener(new MyGestureListener(this));
}
/**
* inheritance GestureListener , the rewrite left and right methods
*/
private class MyGestureListener extends GestureListener {
public MyGestureListener(Context context) {
super(context);
}
@Override
public boolean left() {
Log.e("test", " The left slide ");
return super.left();
}
@Override
public boolean right() {
Log.e("test", " Slide to the right ");
return super.right();
}
}
}
package com.example.test;
import android.content.Context;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.GestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
/**
* Implementation listens for left and right sliding events, which one view Be direct when needed setOnTouchListener It's ready to go
* @author LinZhiquan
*
*/
public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {
/** The shortest distance to slide left or right */
private int distance = 100;
/** Maximum speed of left and right sliding */
private int velocity = 200;
private GestureDetector gestureDetector;
public GestureListener(Context context) {
super();
gestureDetector = new GestureDetector(context, this);
}
/**
* Methods that are called when you slide to the left should be overridden by subclasses
* @return
*/
public boolean left() {
return false;
}
/**
* Methods that are called when you slide to the right should be overridden by subclasses
* @return
*/
public boolean right() {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
// e1 : the first 1 a ACTION_DOWN MotionEvent
// e2 : the last 1 a ACTION_MOVE MotionEvent
// velocityX : X The speed of movement on the axis (pixels) / A second)
// velocityY : Y The speed of movement on the axis (pixels) / A second)
// The left slide
if (e1.getX() - e2.getX() > distance
&& Math.abs(velocityX) > velocity) {
left();
}
// Slide to the right
if (e2.getX() - e1.getX() > distance
&& Math.abs(velocityX) > velocity) {
right();
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gestureDetector.onTouchEvent(event);
return false;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public int getVelocity() {
return velocity;
}
public void setVelocity(int velocity) {
this.velocity = velocity;
}
public GestureDetector getGestureDetector() {
return gestureDetector;
}
public void setGestureDetector(GestureDetector gestureDetector) {
this.gestureDetector = gestureDetector;
}
}