Android gesture recognition

At present, smart phones are basically touch operations. Clicking a button is an interactive mode. At the same time, gesture related operations, such as sliding, are also very important interactive modes. This article is a collation and summary of Android gesture interaction related knowledge points, mainly from official documents.

Concepts in touch interaction

Common events

First, we need to understand some common events: action_ Down: press action with the first finger_ Up: the first finger raises the action_ POINTER_ Down: second, third, fourth, etc. press action with your fingers_ POINTER_ Up: second, third, fourth, etc. raise your finger action_ Move: finger movement action_ Out side: the finger moves out of the screen action_ Cancel: a precursor event such as action is received_ After down, subsequent events are generated when they are intercepted by the parent control

As we can see above, except that the first finger is triggered by the only action down and action up events, the subsequent pressing and moving of other fingers trigger the same event. Then this time may involve the logical processing of the distinction between different fingers.

MotionEvent

In motionevent, action code and coordinate value are used to describe the track of touch motion, action code value describes the change of motion state, and coordinate value describes the position of track and other information. Like action_ Down indicates that the finger begins to touch the screen, and the coordinate axis values of X and Y indicate the current position.

The above is only the basic single finger operation, but now many devices provide the function of multi finger operation. Multiple fingers. Each finger is assigned a pointer ID when touching the screen for the first time. It becomes invalid until the finger leaves the corresponding pointer ID. When the first finger is pressed, action is triggered_ DOWN,ACTION_ Move is a series of events. At the same time, when the second finger is pressed, action will be triggered_ POINTER_ After the down event, only action will be triggered when two fingers move_ Move event. When an action_ When move is triggered, use the getpointerid (the finger) method to get the pointer ID, specify which finger it is, and then use the findpointerindex method to get the pointer index. The pointer index represents which of the motionevent events is the event corresponding to the current pointer.

Motionevent event bundle

Combined with the above concepts, let's talk about the binding of motionevent. In order to improve processing efficiency, Android will bind multiple coordinate points in the move action into a motionevent. For a single finger operation, geTx returns the coordinates of the nearest point and gethistoricalx returns the previous coordinates. Look at the following code:

You can see that a motionevent may include the action information of multiple fingers and some historical information.

Event distribution mechanism

Motionevent represents the response event after touching. The view in Android is constructed according to the view tree. After clicking, the click event motionevent will be generated and passed along the tree.

Methods related to event distribution include: public Boolean dispatchtouchevent (motionevent EV) event distribution public Boolean onintercepttouchevent (motionevent EV) event interception public Boolean ontouchevent (motionevent EV) event response

A ViewGroup usually has the above three methods to distribute, intercept and respond to events. Because there is no child view in a view, it can only process events, that is, there is only ontouchevent method.

dispatchTouchEvent

In the process of event distribution, it will be distributed in the way of deep traversal. It is divided into the following situations:

If true is returned, the event will be distributed to the current view and consumed by the current view. Return false and return the event to the parent view for consumption. The default super.dispatchtouchevent (EV) will call onintercepttouchevent of the current view for interception. In general, instead of rewriting the view distribution process, we focus on event interception and response.

onInterceptTouchEvent

If true is returned, the current event will be intercepted and handled by ontouchevent. If false is returned, the current event will not be intercepted and will be handled by the dispatchtouchevent of the child view. If the default super.onintercepttouchevent is called, the current event will be intercepted.

onTouchEvent

If false is returned, it indicates that the current view cannot be processed, and the ontouchevent with the superior view will be returned for processing, and it will be passed up until the event is consumed. If true is returned, the event will be received and consumed. If super.ontouchevent (EV) is returned, the default processing logic of the event is the same as that when false is returned. Note that for view, not ViewGroup, there is only the ontouchevent method. Therefore, in a view, the typical code for processing event response is as follows:

Similarly, when a view handles touch events, it can also set the listener ontouchlistener. However, it should be noted that ontouchlistener has higher priority than ontouch. If true is returned, the ontouch method will not be called.

Gesture detection

In ontouch, we can obtain the coordinate information of the touch point through motionevent, but we still need to carry out our own logical processing for some gestures such as click and slide. Here, Android itself provides some gesture discrimination functions. In this way, in the ontouch method, we only need to pass the motionevent to the gesture listener for processing, and implement the corresponding callback method in the interface:

If you don't need to listen to so many events, you can write a listening class to inherit gesturedetector.simpleongesturelistener and implement its methods.

If you want to monitor the speed of touch, you can monitor through velocitytracker:

By adding motionevent to velocitytracker, you can calculate the speed through computecurrent velocity. (to be continued...)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>