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, the event is handled by onTouchEvent of the view. If dispatchTouchEvent returns false, the view's interceptTouchEvent method is used to determine whether to intercept the event. If interceptTouchEvent returns true, the event is intercepted, it will be handed over to its onTouchEvent for processing. If interceptTouchEvent returns false, it will be passed to the sub-view, and the dispatchTouchEvent of the sub-view will start this matter again. Distribution of parts. If the event is passed to the onTouchEvent of the subview of a certain layer, the method returns false, and the event will be passed up from this view, which is 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.

 

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.