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

标签 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
}

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

最佳答案

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

关于java - 手指在屏幕上滑动的灵敏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45233537/

相关文章:

java - 如何在 Android 版 Flutter 中渲染 OpenGL?

java - 向前倾斜还是向后倾斜?类型旋转 vector

android - Android应用程序开发中如何使用加速度计测量距离

java - 从 http 客户端获取 404

JavaFX DatePicker 返回 null

android - 如何使用 google maps api 获得速度限制?

c# - Android 通过 LAN 从 C# 桌面应用程序接收 UDP 广播?

opengl-es-2.0 - 传感器方向 -> 旋转无法正常工作

java - Apache Camel XML 到 JSON

java - Android 处理,库的使用