java – 如何在Android touchlistener中同时检测触摸屏幕上的辅助手指?
作者:互联网
如何检测屏幕上的其他手指?例如.我用一根手指触摸屏幕,过了一段时间我将第一根手指放在屏幕上,然后用另一根手指触摸屏幕,同时保持第一根手指的原样?如何在Touch Listener中检测秒针触摸?
解决方法:
从第二个手指开始发送MotionEvent.ACTION_POINTER_DOWN和MotionEvent.ACTION_POINTER_UP.对于第一个手指,使用MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP.
MotionEvent上的getPointerCount()方法允许您确定设备上的指针数.所有事件和指针的位置都包含在您在onTouch()方法中收到的MotionEvent实例中.
要跟踪来自多个指针的触摸事件,您必须使用MotionEvent.getActionIndex()和MotionEvent.getActionMasked()方法来标识指针的索引和此指针发生的触摸事件.
int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;
Log.d(DEBUG_TAG,"The action is " + actionToString(action));
if (event.getPointerCount() > 1) {
Log.d(DEBUG_TAG,"Multitouch event");
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
} else {
// Single touch event
Log.d(DEBUG_TAG,"Single touch event");
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
...
// Given an action int, returns a string description
public static String actionToString(int action) {
switch (action) {
case MotionEvent.ACTION_DOWN: return "Down";
case MotionEvent.ACTION_MOVE: return "Move";
case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
case MotionEvent.ACTION_UP: return "Up";
case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
case MotionEvent.ACTION_OUTSIDE: return "Outside";
case MotionEvent.ACTION_CANCEL: return "Cancel";
}
return "";
}
有关详细信息,请访问Google Handling Multi-Touch Gestures.
标签:java,android,touch,ontouchlistener,android-viewgroup 来源: https://codeday.me/bug/20190609/1203552.html