Android touch Event Processing Mechanism

Source: Internet
Author: User

When a touchevent occurs, the activity first passes the touchevent to the top-level view. The touchevent first reaches the dispatchtouchevent of the top-level view, and then is distributed by the dispatchtouchevent method. If the dispatchtouchevent returns true
If the dispatchtouchevent returns false, the view's intercepttouchevent method is used to determine whether to intercept the event. If intercepttouchevent returns true, It is intercepted, it is handed over to its ontouchevent for processing. If intercepttouchevent returns false, it is passed to the sub-view, and the dispatchtouchevent of the sub-view starts the event distribution. If the event is passed to a child
The ontouchevent of the view is mounted. If this method returns false, the event will be passed up from the view and received by ontouchevent. If false is returned when the ontouchevent is passed to the top, the event will disappear and the next event will not be received.

Ontouch method: This is the method defined in the ontouchlistener interface of the view. When the view is bound with an ontouchlistener, The ontouch method is called when a touch event is triggered.

Ontouchevent method: override the method in activity.

Android touch event processing has three layers: Activity layer, viewgroup layer, and view layer.
Several Basic Principles of touch event processing:
1. If action_down is not processed at a level, the layer will no longer receive subsequent touch events until the next action_down event
Note:
A. A level does not process an event. It means that neither the event nor its subview processes the event.
B. This rule does not apply to the activity layer (which is the top layer). They can receive every touch event.
C. Events such as action_move are not processed and will not be affected.
2. If the action_down event occurs within the range of a view, the subsequent action_move, action_up, action_cancel and other events will be sent to the view, even if the event has already exceeded
3. the first finger is used to trigger the action_down event, and then the pressed finger triggers the action_pointer_down event. The middle finger triggers the action_pointer_up event, the last finger triggers the action_up event (even if it is not the finger that triggers the action_down event ).
4. the pointer ID can be used to trace the finger. The pointer id takes effect from the moment when it is pressed until it becomes invalid.
5. If an action_down event is intercepted by the view, no touch event will be received by any sub-view (this meets the first requirement)
6. if a non-action_down event is intercepted by the parent vew, the child views that last handled the action_down event will receive an action_cancel event and will not receive any touch events, even if the parent view does not intercept subsequent touch events.
7. if the parent view decides to process the touch event or the child view does not process the touch event, the parent view processes the touch event as a normal view, otherwise, it does not process touch events at all (it is only responsible for distribution)
8. If the parent view intercepts an event in onintercepttouchevent, The onintercepttouchevent will no longer receive the touch event, and the event will be handled by itself.


Summary:

Android event triggering process:

-------------------------------------------------------------------

First, trigger the dispatchtouchevent of the activity.

Then, the onuserinteraction of the activity is triggered.

Then, the dispatchtouchevent of layout is triggered.

Then, the onintercepttouchevent of layout is triggered.

-------------------------------------------------------------------

View: http://hi.baidu.com/lck0502/blog/item/7eeb452a846ff196023bf654.html

========================================================== ========================================================== ======================================

In viewgroup (That is, the preceding Layout) Special analysis:

Events triggered by touch.

Android events: onclick, onscroll, onfling, and so on are composed of many touch events. The first status of touch must be action_down, indicating that the screen is pressed. Later, touch will have a follow-up event, which may be:

  • Action_move // indicates a mobile gesture.

  • Action_up // indicates leaving the screen

  • Action_cancel // indicates that the canceling gesture is not generated by the user, but by the program.

One action_down, N action_move, and one action_up constitute many events in Android.

In Android, a type of control can also contain other child controls, which inherit from the viewgroup class, such as listview, gallery, and gridview.

Another type of controls cannot contain child controls, such as textview.

This article mainly discusses the trigger of nested event items of viewgroup controls.

An important method for viewgroup controls is onintercepttouchevent (), which is used to process events and change the transfer direction of events. Its return value is a Boolean value, determines whether the touch event needs to be passed to the Child view it contains. This method is to pass from the parent View to the Child view.

If return is true, it is passed to the ontouchevent () of the current control instead of the Child control. This is called intercept (truncation ).

If return false; (the default value is false), the method is not fully processed,

In this way, the event can be passed to the ontouchevent in the view.

The default ontouchevent value in viewgroup is false.

The default value of ontouchevent returned in the view is true. In this way, multiple touch events can be executed.

The onintercepttouchevent () mechanism is complicated. To sum up, the basic rules are as follows:

1. The down event is first passed to the onintercepttouchevent () method.

2. if the onintercepttouchevent () of the viewgroup returns false after the down event is processed, the subsequent move, up, and other events will be passed to the viewgroup first, the ontouchevent () process is passed to the final target view just like the down event.

3. if the onintercepttouchevent () of the viewgroup returns true after it receives the down event processing, the subsequent move, up and other events will not be passed to onintercepttouchevent (), the ontouchevent () that is passed to the viewgroup like the down event. Note that the target view will not receive any event.

4. If the ontouchevent () of the view that finally needs to process the event returns false, the event will be passed to the ontouchevent () of the view at the previous level for processing. (Nima, return false, passed to the parent view)

5. If the ontouchevent () of the view that finally needs to process the event returns true, subsequent events can be passed to the ontouchevent () of the view for processing.

MethodOntouchevent ()It is used to receive and process events. Its return value is also a Boolean value, which determines whether the event and subsequent events continue to pass up,This method is passed from the Child View to the parent view.

The transfer mechanism between onintercepttouchevent () and ontouchevent and each childview depends entirely on the return values of onintercepttouchevent () and ontouchevent. If the returned value is true, the event is correctly received and processed. If the returned value is false, the event is not processed and will continue to be passed (except that the transfer direction is different,Onintercepttouchevent () is passed to the Child view, while ontouchevent () is passed to the parent view).

The details are as follows:

The action_down event is uploaded to the onintercepttouchevent of a viewgroup class. If false is returned, the down Event continues to be passed to the onintercepttouchevent of the child viewgroup class. If the child view is not a control of the viewgroup class, the ontouchevent that is passed to it.

If onintercepttouchevent returns true, the ontouchevent that the down event is passed to it will not be passed, and subsequent events will also be passed to its ontouchevent.

If the ontouchevent of a view returns false, the down Event continues to be passed to the ontouchevent of its parent viewgroup class. If true is returned, subsequent events will be directly passed to its ontouchevent for further processing. (Subsequent events will only be passed to ontouchevent that returns true for the required event action_down)

To sum up, onintercepttouchevent can accept all touch events, while ontouchevent is not necessarily.

Extracted from: http://www.cnblogs.com/kingcent/archive/2011/03/08/1977059.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.