【问题标题】:The sensitivity of the finger swipe on the screen手指在屏幕上滑动的灵敏度
【发布时间】:2017-07-21 09:04:52
【问题描述】:

我正在使用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
}

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

【问题讨论】:

  • 如果这个值改变了,那么什么都不会改变

标签: java android android-sensors android-gesture


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @jwenting 感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    相关资源
    最近更新 更多