enter code here
需要检测滑动方向。在我的代码中,我可以检测方向,但它会出现像是,如果我滑过右上方或左上方, 。同样的左边,我的要求是没有举手指,如果我向左滑动它应该只剩下,同样所有的方向。谁能帮我吗。多谢!所有方向的滑动手势(左,右,上,下)
@覆盖 公共布尔的onTouchEvent(MotionEvent的TouchEvent){
switch (touchevent.getAction()) {
// when user first touches the screen we get x and y coordinateyo
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_MOVE: {
x2 = touchevent.getX();
y2 = touchevent.getY();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
// Left to Right swipe action
if (x2 > x1) {;
Log.e("RTL", "Right to Left Swap Performed");
}
else {
Log.e("LTR", "Left to Right Swap Performed");
}
} else {
if (y2 > y1) {
Log.e("UTD", "UP to Down Swap Performed");
}
// Right to left swipe action
else {
Log.e("DTU", "Down to UP Swap Performed");
}
}
}
}
return false;
}
http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures https://developer.android.com/training/手势/ detector.html – cokceken
@cokceken移动工作..但我无法得到确切的左,右,上下 – user2806221
我不认为有一个函数,返回方向。你应该做数学。计算移动矢量,计算角度。如果你的矢量大于X,并且你的角度在Z-Y之间,那么用户滑过方向1。 – cokceken