gpt4 book ai didi

java - 我应该如何更改此代码才能更自由地向左/向右滑动?

转载 作者:行者123 更新时间:2023-12-01 14:01:11 26 4
gpt4 key购买 nike

问题是我必须几乎严格水平滑动才能使滑动操作起作用。我希望滑动时有更多的自由度。我希望当我对角滑动时滑动也能起作用。我应该如何更改此代码?

这是代码

 public class OnSwipeTouchListener implements OnTouchListener {  

Context context = null;

private final GestureDetector gestureDetector = new GestureDetector(context, new GestureListener());

public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}



private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}

最佳答案

更改此:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
------> if (Math.abs(diffX) > Math.abs(diffY)) { <------
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {.....

对此:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
------> if (Math.abs(diffX) > Math.abs(diffY) - 50) { <------
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {.....

这将允许您以比对角线稍大一点的距离滑动,并且仍然会被重新标记为左右滑动。

这也将使您必须更直接地向上或向下滑动。

如果您不想想要响应向上滑动,您可以这样做:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;

关于java - 我应该如何更改此代码才能更自由地向左/向右滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19340131/

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