Android touch screen event and motionevent details

Android screen operation

Screen is the main medium for users to interact with Android devices. The screen is divided into touch screen and non touch screen. There are currently four types of Android devices: Android phone, Android Tablet, Android wear and Android TV. Android TV mostly uses non touch screen, and most of the other three types of devices use touch screen. For non touch screen devices, users can operate on the screen through keyboard, mouse or remote control. For touch-screen devices, users mainly operate on the screen through tools such as fingers or stylus. Of course, they can also operate through external keyboard, mouse, trackball and other tools.

Android screen interaction events

All user operations on the device screen will be converted into various screen interaction events. Android screen interaction events mainly include the following types.

In order to facilitate understanding and simplify the description, finger operation will be used to refer to all touch screen operations. For example, "this event occurs when a finger touches the screen" does not mean that this event can only occur when a finger touches the screen, but should be understood as "this event will occur when a finger, stylus and other tools touch the screen".

Touch screen event type

According to actions, touch screen events can be divided into the following three categories

Among them, the finger pressing on the screen and the finger leaving the screen must appear in pairs. In the middle, there will be an indefinite number of finger moving events on the screen.

Touch screen event sequence

In the Android system, a series of touch screen events generated in the process from the finger pressing on the screen to the finger leaving the screen constitute an event sequence (also known as event flow). For multi-point touch screen events, it starts from the first finger pressing on the screen to the last finger leaving the screen.

A touch screen event sequence. The first event must be the finger pressing on the screen, and the last event must be the finger leaving the screen. All user touch-screen operations on the device screen will eventually be converted into several such event sequences.

It is very important to understand the concept of touch screen event sequence. The handling of touch screen events in Android often needs to be investigated in the unit of event sequence.

Representation of Android touch screen events in code

In the Android system, motionevent objects are used to represent a touch-screen event. When users operate on the screen with their fingers, a series of motionevent objects will be generated. However, it should be noted that the generation of a motionevent object does not necessarily mean that it is a touch-screen operation. Motionevent can be used to represent not only touch event, but also hover event and scroll event. That is, other screen interaction events except key event are represented by motionevent (key event is represented by keyevent object).

In the motionevent class, the action that generates this event is called motion, and the body that generates this action (such as finger, mouse, etc.) is called pointer. A motionevent object can contain one or more pointers. Each pointer contains attributes such as ID, index, location, size, direction, etc. In multiple events of a touch screen event sequence, the same pointer has the same ID, but the index can be different.

Only the part about touch event in motionevent is discussed here. The motionevent object mainly contains the following information:

1. Action code

Motionevent provides getactionmasked () method to get the type of this operation, which is an int value. In addition to getactionmasked (), there is a getaction () method. The difference between getactionmasked () and getactionmasked () will be described later.

A series of int constants are defined in the motionevent class to represent various predefined operation types. Listed below.

According to the above description of touch screen event sequence, a normal touch screen event sequence must be based on action_ Start with down and start with action_ Up is the end, and there can be 0 or more actions in the middle_ Move, if it is multi touch, there will be several actions in the middle_ POINTER_ Down and action_ POINTER_ UP。 ACTION_ POINTER_ Down and action_ POINTER_ Up must be relative in quantity.

An early terminated touch screen event sequence must be action_ Start with down and start with action_ Cancel is the end, and there can be 0 or more actions in the middle_ Move, if it is multi touch, there will be several actions in the middle_ POINTER_ Down and action_ POINTER_ UP。 ACTION_ POINTER_ Down and action_ POINTER_ The number of UPS may vary.

The difference between getaction() and getactionmasked(): action_ POINTER_ Down and action_ POINTER_ For events other than up, the return value of getaction () is the same as getactionmasked (). Yes, action_ POINTER_ Down and action_ POINTER_ Up, the return value of getaction () is slightly different from that of getactionmasked (). The return value of getaction () contains the operation type and the pointer index corresponding to the pointer that generated this event. The lower 8 bits represent the operation type and the higher 8 bits represent the pointer index.

2. Pointer information

In addition, there are gettoolmajor(), gettoolminor(), gettouchmajor(), gettouchminor(), getorientation() and other methods to obtain the area size, direction and other information of the pointer. Due to less actual use, it will not be introduced here.

3. Operation time

You can get the time when this event occurs through the geteventtime () method of the motionevent class.

4. Historical data of event sequence

Some historical event information of the event sequence in which it is located will also be saved in the motionevent object. You can obtain the number of historical event records through gethistorysize() and obtain the information of historical events through a series of gethistoricalxxx() methods. Due to action_ Down is the beginning of an event sequence, so action_ There will be no historical event records in the event object corresponding to down. There will be 0 to more records of historical event information in the motionevent object corresponding to subsequent events. The number of specific records is not fixed, and the total number will not be too much.

Among the above information, the first two are mostly used, that is, the type of event and the relevant information of pointer when the event is generated.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>