我有一个 Android 应用程序,我希望它在周围画圈
我使用了 OnTouchListener。
问题是,当用户将圆圈“固定”到位时,它不会更新操作。
我如何知道用户的手指是否打开,当他不移动它时,使用 OnTouchListener
我有一个 Android 应用程序,我希望它在周围画圈
我使用了 OnTouchListener。
问题是,当用户将圆圈“固定”到位时,它不会更新操作。
我如何知道用户的手指是否打开,当他不移动它时,使用 OnTouchListener
你可以知道,通过使用
MotionEvents:
里德更多:MotionEvents
ACTION_DOWN is for the first finger that touches the screen. This starts the gesture. The pointer data for this finger is always at index 0 in the MotionEvent.
ACTION_POINTER_DOWN is for extra fingers that enter the screen beyond the first. The pointer data for this finger is at the index returned by getActionIndex().
ACTION_POINTER_UP is sent when a finger leaves the screen but at least one finger is still touching it. The last data sample about the finger that went up is at the index returned by getActionIndex().
ACTION_UP is sent when the last finger leaves the screen. The last data sample about the finger that went up is at index 0. This ends the gesture.
ACTION_CANCEL means the entire gesture was aborted for some reason. This ends the gesture.
您必须检查是否触发了 onTouchEvent 类型的事件。
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do Nothing
return true;
case MotionEvent.ACTION_MOVE:
//Do Something
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
//Do Nothing
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
可以在此处找到有关 MotionEvents 的更多信息。
按触摸类型过滤触摸:
boolean onScreen = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX(); //X coord of the touch
float eventY = event.getY(); //Y coord of the touch
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// User has touched the screen
onScreen = true;
return true;
case MotionEvent.ACTION_MOVE:
// Finger is being dragged on the screen
onScreen = true; //Not required, just for clarity purposes.
break;
case MotionEvent.ACTION_UP:
// User has lifter finger
onScreen = false;
break;
default:
return false;
}
if(onScreen) {
//Finger is on the screen
}
return true;
}