Android Touch Event Delivery mechanism

Source: Internet
Author: User

-----Source: http://www.trinea.cn/android/touch-event-delivery-mechanism/

Describes the delivery mechanism for Android touch events .

Many friends of the private message asked this question, then recommend a I see the transfer mechanism to introduce the most clear foreign articles it. This article is slightly translated.

1. Basic knowledge

(1) All touch events are encapsulated as motionevent objects, including touch location, time, history, and the first few fingers (multi-fingered touch).

(2) The event types are divided into Action_down, action_up, Action_move, Action_pointer_down, Action_pointer_up, Action_cancel, and each event is Action_ Down begins action_up end.

(3) The handling of events consists of three classes, namely the transfer--dispatchtouchevent () function, The Intercept--onintercepttouchevent () function, the consumption--ontouchevent () function, and the Ontouchlistener

2. Delivery process

(1) The event is passed from Activity.dispatchtouchevent (), as long as it has not been stopped or intercepted, and is passed down (sub view) from the topmost view (ViewGroup). The child view can handle the event through Ontouchevent ().

(2) The event is passed by the parent view (ViewGroup) to the child View,viewgroup can intercept the event by Onintercepttouchevent () and stop it from passing down.

(3) If the event from the top down the process has not been stopped, and the bottom of the sub-view no consumption events, the event will be anti-yearning to pass, then the parent view (ViewGroup) can be consumed, if still not consumed, The Ontouchevent () function of the activity is finally reached.

(4) If the view does not consume the action_down, then other events will not be passed over.

(5) Ontouchlistener takes precedence over ontouchevent () to consume the event.

The above consumption means that the corresponding function return value is true.

read more directly in PDF: Mastering the Android Touch System, sample code:[email protected]. What's the problem can leave a message here.

Attach two flow charts in the original text

(1) View does not handle event flowchart

View-ignore-touch-event-example

(2) View processing event flowchart

-----Source: http://www.trinea.cn/android/touch-event-delivery-mechanism/

Callout: Touch's event Stream makes a demo verification of itself: (Demo address: Https://github.com/alicx/leaf8/tree/androidDemo)

The touch event is passed from the dispatch of the activity to the dispatch of ViewGroup, and Dispatch is called first in the onintercepttouchevent of ViewGroup.

A1: If the method returns true, it means that ViewGroup will intercept the touch event, and subsequent calls to ViewGroup's ontouchlistener/ontouchevent (* superscript 1*ontouchlistener will always take precedence
The Ontouchevent method is called, and if the former returns true then the latter will not be called).
A2: If the method returns False, the Dispatch,view will continue to be passed to view as a child node without the Intercept method, and the ontouchlistener/ontouchevent will be executed directly (e.g. superscript 1)
Basic compliance with on-diagram processes

(EXT) Android Touch event delivery mechanism

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.