如果设备上有多个手指,onTouch-listener是否会为每个手指运行一次,或者每个事件运行一次(手指向下,移动等等)并包含每个活动的触摸指针?
如果
(e.getAction() & MotionEvent.ACTION_MASK == MotionEvent.ACTION_MOVE)
我如何知道当前哪个指针正在移动?
final int pointerIndex = e.getActionIndex();
pointerID = e.getPointerId(pointerIndex);
由于某种原因,总是返回0(而MotionEvent.ACTION_MOVE)..
答案 0 :(得分:3)
首先,你得到这些事件:
MotionEvent.ACTION_DOWN
MotionEvent.ACTION_POINTER_DOWN
MotionEvent.ACTION_UP
MotionEvent.ACTION_POINTER_UP
MotionEvent.ACTION_MOVE
这样做:
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; //index of this pointer
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
//your first touch on the screen
break;
case MotionEvent.ACTION_MOVE:
//one of the touches has moved
for (unsigned int i = 0; i < ev.getPointerCount(); i++)
{
//The pointer id is ev.getPointerId(i);
//for loop through all touches
}
break;
case MotionEvent.ACTION_UP:
//your last touch has gone up
break;
case MotionEvent.ACTION_POINTER_DOWN:
//non-primary pointer has gone down
break;
case MotionEvent.ACTION_POINTER_UP:
//non-primary pointer has gone up
break;
}
}
return true;
}
我希望这会对你有所帮助:)。