手指在屏幕上滑动的灵敏度

时间:2017-07-21 09:04:53

标签: java android android-sensors android-gesture

当用户在指定方向上将手指移过屏幕时,我使用GestureDetector.OnGestureListener来调用方法。 我的问题 - 只需稍微移动并调用方法,我希望用户在屏幕上多移一点手指。

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
}

有可能吗?我应该在这里更改或添加什么?

2 个答案:

答案 0 :(得分:0)

您需要计算您不希望在滑动时执行任何操作的x轴或y轴的等级。

答案 1 :(得分:0)

您必须声明滑动的最小距离,private static final int SWIPE_MIN_DISTANCE = 160可能会帮助您Click here to implement it