2

如果设备上有多个手指,onTouch-listener 是为每个手指运行一次,还是每个事件运行一次(手指向下、移动等)并包含每个活动的触摸指针?

而如果

(e.getAction() & MotionEvent.ACTION_MASK == MotionEvent.ACTION_MOVE)

我怎么知道当前哪个指针在移动?

final int pointerIndex = e.getActionIndex();
pointerID = e.getPointerId(pointerIndex);

由于某种原因,总是返回 0(而 MotionEvent.ACTION_MOVE)..

4

1 回答 1

3

首先,你会得到这些事件:

  1. MotionEvent.ACTION_DOWN
    第一次触摸屏幕
  2. MotionEvent.ACTION_POINTER_DOWN
    对于所有非主要触摸
  3. MotionEvent.ACTION_UP
    最后一次润色
  4. MotionEvent.ACTION_POINTER_UP
    对于每个非主要修饰
  5. 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;
}

我希望这对你有一点帮助:)

于 2013-01-07T18:32:41.597 回答