我正在开发一个我需要的应用程序。用户的方向在Action.Down中将手指拖到屏幕上(在他抬起手指之前) 如果我编码了一些代码,但只有在用户从移动活动中抬起手指后才会告诉值。但我希望用户的动作方向,而他的手指放在屏幕上
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// store the X value when the user's finger was pressed down
downXValue = event.getX();
downYValue = event.getY();
Log.d("recording", "= " + downYValue);
break;
}
case MotionEvent.ACTION_UP: {
// Get the X value when the user released his/her finger
float currentX = event.getX();
float currentY = event.getY();
// check if horizontal or vertical movement was bigger
if (Math.abs(downXValue - currentX) > Math.abs(downYValue
- currentY)) {
Log.d("recording", "x");
// going backwards: pushing stuff to the right
if (downXValue < currentX) {
Log.d("recording", "right");
}
// going forwards: pushing stuff to the left
if (downXValue > currentX) {
Log.d("recording", "left");
}
} else {
Log.d("recording", "y ");
if (downYValue < currentY) {
Log.d("recording", "down");
}
}
if (downYValue > currentY) {
Log.d("recording", "up");
}
}
break;
}
}
return true;
}
});
答案 0 :(得分:1)
使用MotionEvent.ACTION_MOVE
代替MotionEvent.ACTION_UP
<强> ACTION_MOVE 强>
getActionMasked()的常量:在按下期间发生了更改 手势(在ACTION_DOWN和ACTION_UP之间)。动议包含 最近的一点,以及自上次以来的任何中间点 向下或移动事件。
<强> ACTION_UP 强>
getActionMasked()的常量:按下的手势已经完成, motion包含最终发布位置以及任何中间位置 自上次下跌或移动事件以来的点数。
试试这个:
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// store the X value when the user's finger was pressed down
downXValue = event.getX();
downYValue = event.getY();
Log.d("recording", "= " + downYValue);
break;
}
case MotionEvent.ACTION_MOVE: {
// Get the X value when the user released his/her finger
float currentX = event.getX();
float currentY = event.getY();
// check if horizontal or vertical movement was bigger
if (Math.abs(downXValue - currentX) > Math.abs(downYValue
- currentY)) {
Log.d("recording", "x");
// going backwards: pushing stuff to the right
if (downXValue < currentX) {
Log.d("recording", "right");
}
// going forwards: pushing stuff to the left
if (downXValue > currentX) {
Log.d("recording", "left");
}
} else {
Log.d("recording", "y ");
if (downYValue < currentY) {
Log.d("recording", "down");
}
}
if (downYValue > currentY) {
Log.d("recording", "up");
}
}
break;
}
}
return true;
}
});