Android:如何让滑动屏幕导航变得舒适

发布于 2024-10-08 19:48:10 字数 1356 浏览 3 评论 0原文

我正在使用手势列表器来实现滑动屏幕导航。但它不太舒服,我需要多次滑动才能导航到下一个或上一个屏幕。速度、偏离路径和最小距离的正确值是多少?

请找到下面是我的代码:

private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 300;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideLeftIn);
                viewFlipper.setOutAnimation(slideLeftOut);
                viewFlipper.showNext();    
                configDisplay();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideRightIn);
                viewFlipper.setOutAnimation(slideRightOut);
                viewFlipper.showPrevious();
                configDisplay();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

I am using Gesture Listner to acheive Swipe screen navigation. But it is not so comfortable that I need to swipe multiple time to navigate to next or previous screen. What are the proper values for Velocity, Off path and Min Distance.

Please find below is my code:

private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 300;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideLeftIn);
                viewFlipper.setOutAnimation(slideLeftOut);
                viewFlipper.showNext();    
                configDisplay();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideRightIn);
                viewFlipper.setOutAnimation(slideRightOut);
                viewFlipper.showPrevious();
                configDisplay();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-10-15 19:48:10

我最近实现了一个使用 flinging 的应用程序,并从 SimpleOnGestureListener 开始,就像您的示例一样。我记录了每次投掷的速度和偏移量,并在我的设备上进行测试,以找到我认为合适的值。

但是 - 我强烈建议您查看 ViewPager,它可以在 兼容包 代替。

关于 ViewPager 以及示例链接。

I recently implemented an app that used flinging and started out with SimpleOnGestureListener just like your example. I logged velocity and offset on each fling and tested on my device to find what I thought was suitable values.

But - I highly recommend you to take a look at the ViewPager which is available in the compability package instead.

About ViewPager with link to example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文