3

MotionEvent's documentation states in Consistency Guarantees that: "(For touch events)... pointers go down one at a time, move around as a group and then go up one at a time or are canceled".

If I understand correctly, there are two ways one can determine the action that triggered onTouchEvent():

  • MotionEvent.getAction()
  • MotionEvent.getActionMasked() with MotionEvent.getActionIndex() (this should be used for multitouch, which is what I'm after)

Since getActionMasked() always returns just ACTION_POINTER_DOWN and ACTION_POINTER_UP, according to Consistency Guarantees there should always be just one pointer passed inside MotionEvent to onTouchEvent(), which means that MotionEvent.getActionIndex() will always return 0.

If that is so, what is the point of having MotionEvent.getActionIndex() at all? In other words: what am I missing?

UPDATE: To further clarify my question: MotionEvent.getActionMasked() returns just one action and MotionEvent.getActionIndex() tells us which pointer it applies to. Does that mean that we can't get the action for other pointers? Or if it is the same for all pointers, why specify actionIndex at all?

johndodo
  • 17,247
  • 15
  • 96
  • 113

3 Answers3

2

The event ACTION_POINTER_DOWN means two or more fingers are now touching the screen, to distinguish whether it is the second or third (etc) finger you need to use getActionIndex().


Since getActionMasked() always returns just ACTION_POINTER_DOWN and ACTION_POINTER_UP

In case there is any confusion, getActionMasked() returns many more types like ACTION_DOWN, ACTION_MOVE, etc.

Sabeeh
  • 1,123
  • 9
  • 11
Sam
  • 86,580
  • 20
  • 181
  • 179
  • But if ACTION_POINTER_DOWN means that two (or more) fingers are touching the screen, why would I need to distinguish between them? They are both touching the screen so action applies to both of them, right? – johndodo Nov 25 '12 at 07:00
  • Thanks Sam for the help, I think I figured it out! – johndodo Nov 25 '12 at 07:04
  • To answer your question: yes the same action is fired for both of them, but you need to use `getActionIndex()` to distinguish between a two finger gesture and a three finger gesture. (If your device can only recognize two fingers, then `getActionIndex()` doesn't really matter.) – Sam Nov 25 '12 at 07:16
  • Actually, two events are fired with two different actions. First with `ACTION_DOWN` and a single pointer, then with `ACTION_POINTER_DOWN`, two pointers and `getActionIndex()` denoting the second pointer. Of course, if I understand correctly - but it does make sense now. – johndodo Nov 25 '12 at 08:59
  • Sounds like you understand it now. – Sam Nov 25 '12 at 17:14
0

I have figured it out with the help of this excellent answer.

Function getActionMasked() gets the action that either applies to all of the pointers or to just one of them (if action is ACTION_POINTER_DOWN and ACTION_POINTER_UP), which you can get with getActionIndex().

The piece of information that I was missing was that while you get masked action ACTION_POINTER_DOWN which applies to only one of the pointers, the other pointers are still provided in the call. But you are guaranteed that there is at most one ACTION_POINTER_DOWN or ACTION_POINTER_UP present in each call to onTouchEvent().

Community
  • 1
  • 1
johndodo
  • 17,247
  • 15
  • 96
  • 113
0

getActionIndex() returns the index of the pointer on two major events, i.e., DOWN and UP. In all other actions, such as ACTION_MOVE, it always returns the pointer at index 0.

So, if we want to implement some specific actions in against MOVE, we have to use the pointers index instead of getting ActionIndex.

user3458211
  • 141
  • 2
  • 1