当用户用2个手指触摸屏幕时,我调用方法finger2()
并放大并缩小(ScaleGestureDetector.OnScaleGestureListener)
它有效。
但最近我添加了新方法finger1()
,它通过触摸触摸屏幕并向左或向右移动(GestureDetector.OnGestureListener)
- 此方法仅检测到1个手指。它也有效。
现在我有问题当用户想要将2个手指放在屏幕上时,首先是从1个手指调用方法然后从2个手指调用方法。
如何解决这个失败?我想在被两根手指触摸时不会触发finger1()
方法。
public class SwipeListener extends GestureDetector.SimpleOnGestureListener {
public static final int MIN_SWIPE_DISTANCE = 40;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float xDiff = e1.getX() - e2.getX();
float yDiff = e1.getY() - e2.getY();
return resolveSwipe(xDiff, yDiff);
}
private boolean resolveSwipe(float xDist, float yDist) {
float yDistAbs = Math.abs(yDist);
float xDistAbs = Math.abs(xDist);
SwipeDirection swipeDirection;
if (yDistAbs > xDistAbs) {
if (yDistAbs < MIN_SWIPE_DISTANCE) {return false;}
swipeDirection = (yDist > 0) ? SwipeDirection.DOWN: SwipeDirection.UP;
} else {
if (xDistAbs < MIN_SWIPE_DISTANCE) {return false;}
swipeDirection = (xDist > 0) ? SwipeDirection.RIGHT: SwipeDirection.LEFT;
}
onSwipeEvent(swipeDirection);
return true;
}
private void onSwipeEvent(SwipeDirection swipeDirection) {
if (swipeDirection == SwipeDirection.UP) {
return;
}
if (swipeDirection == SwipeDirection.DOWN) {
return;
}
if (swipeDirection == SwipeDirection.LEFT) {
finger1()
return;
}
if (swipeDirection == SwipeDirection.RIGHT) {
finger1();
return;
}
}
public enum SwipeDirection {
UP, DOWN, LEFT, RIGHT
}
}