Android Motion Event Detailed Explanation and Handling Views’ onTouchEvent

What each action mask means, empty pointers array and few words about the JNI & native parts

Elvina Sh
ProAndroidDev

--

Image by Alina Starovoitova from Dribbble

Hi there! In the Navigine team, we’ve been providing indoor and outdoor positioning mobile technologies that enable advanced indoor navigation and proximity solutions for eight years.

Recently, we have devoted a lot of time to working with graphics on mobile devices and handling gestures. Today’s article will be devoted to the processing of these gestures, errors that can be caught during this process and in the end, we will tell you how to move these gestures into native code, if you suddenly need this, because there are very few answers on the Internet about that. So let’s dive into it!

Motion Event Explanation

So let’s start with the explanation of the motion event. Here is how Google explains it. Motion events describe movements in terms of an action code and a set of axis values. The action code specifies the state change that occurred such as a pointer going down or up. The axis values describe the position and other movement properties.

Image by Yasir Eryilmaz from Dribbble

In short, a motion event contains information about what kind of action happened and gives the coordinates of these actions. For example, when the user first touches the screen, the system delivers a touch event to the appropriate view with the action code ACTION_DOWN and a set of axis values that include the X and Y coordinates of the touch and information about the pressure, size, and orientation of the contact area.

Now each device supports multi-touch screens and if you want you can use it for better effect. So in this case, there are pointers for each finger or other objects that generate movement and motion event will contain information about all the active pointers even if some of them was not moved in last events. Pointers count will be changed by pointers go down and up, if only the gesture is not canceled, in this case, pointers count will be reset to zero.

Actions and Pointers

Let’s take a closer look at the types of motion event actions and try to figure out how to correctly use them. There are a lot of action types, some of them already deprecated, so we will review the most important ones. The first in id and in importance is the down action. It has the id equal to zero and will be handled when the first finger goes down. The opposite of its up action has an id equal to 1 and will mean that the gesture has finished.

Move actions will happen between the down and up actions and will provide information about each gesture event for each active pointer. Cancel action will inform you that gesture has been aborted and outside action will happen when you will touch any area outside your view. Pointer down and pointer up actions will happen when more than one finger or object will touch the screen.

Now let’s try to understand how to work with pointers. Pointers count will inform you how many fingers or objects touched the screen. But there occur difficulties with getting the correct index when some pointer is up. The code above will help you with this. Pointers will do you a good service in working with multi-touch events, like zooms, double and multitaps with one and few fingers, and other trendy gestures. The next important step is checking that your pointer is active in this touch event because if not you could get some exception or use another pointer data.

public final int findPointerIndex(int pointerId);

Using the function above you could get the pointer index in the current event, it will return -1 if it not exists. This could happen when your finger is not moved for example or when it is already up.

Native Motion Events

In some cases, it may be necessary to process all touch events in native code, for example, if you decide to write your own view and use different tools for this, like OpenGL or something like that. You never know what thoughts arise in developers’ heads, maybe they just want to complicate themselves lives. By the way, such a solution could arise, and you could find something helpful once in a blue moon. So we decided to provide you some information about this process.

Unfortunately, motion events could not be converted to AInputEvent on the JNI part. You will need to do some programming for getting your motion event in the C++ code. But it’s not as difficult as it seems, although it doesn’t look so compact. Here is the code example above, you can extend it for using other functions you needed.

Conclusion

In a nutshell, motion events are a very interesting tool which in the right hands can turn your UI into something beautiful and bewitching. However, when working with them, you should be attentive and careful, otherwise, the exceptions will appear at every turn. We hope the article was useful to you. Feel free to ask your questions and share your motion events using experience.

--

--

Marketing associate at Navigine.com — Integrated software platform for precise indoor and outdoor positioning.