Dávid Tóth
Dávid Tóth

Reputation: 3235

How does the OnTouch event behave?

If there are multiple fingers on the device, does the onTouch-listener run once for every finger, or it runs once per event(finger down, move, etc..) and include every active touch pointer?

And if

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

how do I know which pointer is moving currently?

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

Always gives back 0(while MotionEvent.ACTION_MOVE) for some reason..

Upvotes: 2

Views: 493

Answers (1)

bricklore
bricklore

Reputation: 4165

First, you get these events:

  1. MotionEvent.ACTION_DOWN
    for the FIRST touch on the screen
  2. MotionEvent.ACTION_POINTER_DOWN
    for all non-primary touches
  3. MotionEvent.ACTION_UP
    for the last touch up
  4. MotionEvent.ACTION_POINTER_UP
    for every non-primary touch up
  5. MotionEvent.ACTION_MOVE
    everytime anyone of the touched fingers moves
    here you have to go through all figers and check if the coordinates have changed

do it like this:

@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;
}

I hope this helps you a little bit :)

Upvotes: 3

Related Questions