如何检测屏幕上的额外手指?例如,我用一根手指触摸屏幕,一段时间后我将第一根手指放在屏幕上,然后用另一根手指触摸屏幕,同时保持第一根手指保持原样?如何在 Touch Listener 中检测第二个手指触摸?
问问题
2269 次
2 回答
4
从第二MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP
根手指开始发送。使用第一根手指MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP
。
MotionEvent上的getPointerCount()方法允许您确定设备上的指针数量。所有事件和指针的位置都包含在您在方法中接收的 MotionEvent 实例中。onTouch()
要跟踪来自多个指针的触摸事件,您必须使用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处理多点触控手势。
于 2015-08-17T08:25:53.240 回答
1
如果您需要处理多点触控事件,您应该检查 PointerCount。
public boolean onTouchEvent(MotionEvent event) {
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);
}
...
}
您可以在此处找到更多信息: https ://developer.android.com/training/gestures/multi.html
于 2015-08-17T07:29:38.093 回答