Android:如何让滑动屏幕导航变得舒适
我正在使用手势列表器来实现滑动屏幕导航。但它不太舒服,我需要多次滑动才能导航到下一个或上一个屏幕。速度、偏离路径和最小距离的正确值是多少?
请找到下面是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近实现了一个使用 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.
这些链接帮助我实现了解决方案:
GestureDetector.SimpleOnGestureListener
实现-pinch-zoom-gestur
Android-pinch< /a>
These links helped me achieve the solution:
GestureDetector.SimpleOnGestureListener
implementing-the-pinch-zoom-gestur
Android-pinch