Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter MotionEvent.getAction() on API level 3 (no ACTION_MASK present)

Tags:

android

Im writing an OnTouchListener. I found out that I can check for the ActionType by using bit-operations like

if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE)

however MotionEvent.ACTION_MASK is not present in Android 1.5 (API level 3) How is/was it done there?

like image 913
unR Avatar asked Oct 10 '22 19:10

unR


1 Answers

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.

In Android 1.5 / API level 3, we did not have support in the MotionEvent class for multiple pointers (i.e. multitouch). For a single pointer event, the pointer bits are not set. You can therefore just compare the event with the desired action constant to obtain the check for the particular action:

if ((event.getAction() == MotionEvent.ACTION_MOVE) {...}

In the unlikely event that this does not work, you can try to define your own action mask constant to be 255 and bitwise-AND it with the action as in later API versions.

On the docs for MotionEvent (or indeed any Android reference page), there is a checkbox to filter by API level. If you select API level 3, you can see the state of that class at the time. All of the methods and constants related to the different pointers are greyed out.

like image 54
antonyt Avatar answered Oct 13 '22 12:10

antonyt