The difference of using MotionEvent.getAction() method
Asked Answered
A

1

16

What is the difference between the two approaches below?

 int action1 = event.getAction() & MotionEvent.ACTION_MASK;

 int action2 = event.getAction();
Amerson answered 9/5, 2013 at 14:28 Comment(1)
Note: event.getAction() & MotionEvent.ACTION_MASK is the same as getActionMasked(). See this question, also.Reshape
A
17

The ACTION_MASK is used to separate the actual action and the pointer identifier (e.g. first finger touched, second finger touched, etc.) The first 8 bits of the value returned in getAction() is the actual action part, and so when you bitwise-AND it with the action mask (= 11111111 = 255 = 0xff), you are left with only the action and none of the pointer information.

Keep in mind here that & is used as an arithmetic operator (bitwise) and not a logical operator (single & is a perfectly valid logical operator in Java, as is &&).`

Abagail answered 9/5, 2013 at 15:7 Comment(2)
Ok, but I dont know whats the point of masking since I can compare directly action2 with MotionEvent.ACTION_MOVEAmerson
You don't need to use it if you aren't supporting multitouch in most cases.Abagail

© 2022 - 2024 — McMap. All rights reserved.