首先在activity中声明手势事件
private GestureDetector gd;
再在activity中给当前Activity设置触摸事件监听方法
//给当前Activity设置触摸事件监听方法
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
最后在onCreat方法中实例化手势检测器
gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
//按下
@Override
public boolean onDown(MotionEvent e) {
return false;
}
//按下,但是手指还未抬起时调用此方法
@Override
public void onShowPress(MotionEvent e) {
}
//轻按,按一下,立刻抬起
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
//滚动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
//长按
@Override
public void onLongPress(MotionEvent e) {
}
//拖动
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
});