如何在Android手机中捕捉手指移动方向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture finger movement direction in android phone?
提问by understack
I want to capture the finger movement direction on Android touch phone. If a user slides his finger in up/down/left/right direction, I want to capture this direction. How can I find this? Thanks.
我想在 Android 触摸手机上捕捉手指移动方向。如果用户在上/下/左/右方向滑动他的手指,我想捕捉这个方向。我怎样才能找到这个?谢谢。
回答by cstack
Implement onTouchEvent(), and calculate dx and dy by where the user presses down and lifts up. You can use these values to figure out the direction of the move.
实现onTouchEvent(),通过用户按下和抬起的位置计算dx和dy。您可以使用这些值来确定移动的方向。
float x1, x2, y1, y2, dx, dy;
String direction;
switch(event.getAction()) {
case(MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case(MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
dx = x2-x1;
dy = y2-y1;
// Use dx and dy to determine the direction of the move
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0)
direction = "right";
else
direction = "left";
} else {
if(dy>0)
direction = "down";
else
direction = "up";
}
}
}
回答by nicholas.hauschild
Your best bet would be to deal with the MotionEvent's you get from a View.OnTouchListener() callback. The motion events keep track of how you are currently interacting with the View by its action property.
最好的办法是处理从 View.OnTouchListener() 回调中获得的 MotionEvent。运动事件通过其 action 属性跟踪您当前与视图的交互方式。
I would imagine you can calculate which direction someone slides their finger by examining the action property of the MotionEvents and the x/y values of where the motion event took place.
我想你可以通过检查 MotionEvents 的 action 属性和运动事件发生位置的 x/y 值来计算某人滑动手指的方向。
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldX= event.getX();
oldY= event.getY();
break;
case MotionEvent.ACTION_MOVE:
int newX = motionEvent.getX();
int newY = motionEvent.getY();
int deltaX = oldX - newX;
int deltaY = oldY - newY;
if(Math.abs(deltaY)>Math.abs(deltaX))
//Motion in Y direction.
else
// Motion in X direction.
break;
}
There are lots of other method available on the MotionEvent object for use as well: http://developer.android.com/reference/android/view/MotionEvent.html
MotionEvent 对象上还有许多其他方法可供使用:http: //developer.android.com/reference/android/view/MotionEvent.html
回答by Akbolat SSS
I think instead of having bunch of variables better to use VelocityTracker
我认为与其有一堆变量不如使用VelocityTracker
Here is modified example from Developer Android
这是来自开发人员 Android 的修改示例
private var mVelocityTracker: VelocityTracker? = null
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
// Reset the velocity tracker back to its initial state.
mVelocityTracker?.clear()
// If necessary retrieve a new VelocityTracker object to watch the
// velocity of a motion.
mVelocityTracker = mVelocityTracker ?: VelocityTracker.obtain()
// Add a user's movement to the tracker.
mVelocityTracker?.addMovement(event)
}
MotionEvent.ACTION_MOVE -> {
mVelocityTracker?.run {
val pointerId: Int = event.getPointerId(event.actionIndex)
addMovement(event)
// When you want to determine the velocity, call
// computeCurrentVelocity(). Then call getXVelocity()
// and getYVelocity() to retrieve the velocity for each pointer ID.
computeCurrentVelocity(1000)
val xVelocity = getXVelocity(pointerId)
val yVelocity = getYVelocity(pointerId)
if (abs(xVelocity) > abs(yVelocity))
if (xVelocity > 0)
// right
else
// left
else
if (yVelocity > 0)
// down
else
// up
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
// Return a VelocityTracker object back to be re-used by others.
mVelocityTracker?.recycle()
mVelocityTracker = null
}
}
return true
}