gpt4 book ai didi

java - 根据速度或其他变量向左、向右、向上和向下滑动

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:16 27 4
gpt4 key购买 nike

我有一个从简单手势扩展的类,我正在使用 onfling 方法:

class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
float e1_X = e1.getX();
float e1_Y = e1.getY();
float e2_X = e1.getX();
float e2_Y = e2.getY();
if(velocityX > 0 && velocityX > velocityY){
text.setText("Swipe left");
}else if(velocityX < 0 && velocityX < velocityY){
text.setText("Swipe right");
}else if(velocityY < 0 && velocityX > velocityY){
text.setText("Swipe down");
}else if(velocityY > 0 && velocityX < velocityY){
text.setText("Swipe up");
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}

我知道这取决于某些角度,但我做不到,我尝试使用 velocityX 和 velocityY,只有在精确操作时它才有效。但我想要的是一个“错误”的角度:如果你对角线滑动,例如向上和向右,我需要选择哪个是好的方式。

最佳答案

您应该检查速度和距离。这是水平滑动检测器的示例。您可以以相同的方式添加垂直检测。

public class HSwipeDetector extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; }

/* positive value means right to left direction */
final float distance = e1.getX() - e2.getX();
final boolean enoughSpeed = Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY;
if(distance > SWIPE_MIN_DISTANCE && enoughSpeed) {
// right to left swipe
onSwipeLeft();
return true;
} else if (distance < -SWIPE_MIN_DISTANCE && enoughSpeed) {
// left to right swipe
onSwipeRight();
return true;
} else {
// oooou, it didn't qualify; do nothing
return false;
}
}

protected void onSwipeLeft() {
// do your stuff here
}

protected void onSwipeRight() {
// do your stuff here
}
}

关于java - 根据速度或其他变量向左、向右、向上和向下滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com