Android Touch Event Processing Mechanism

Source: Internet
Author: User

Android's Touch event processing mechanism is complicated, especially after considering multi-point Touch and event interception.
Android Touch event processing has three layers: Activity layer, ViewGroup layer, and View layer.
First, let's talk about several basic rules of Touch event processing.
If the ACTION_DOWN event 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) and can receive every Touch event.
C. Events such as ACTION_MOVE are not processed and will not be affected.

If the ACTION_DOWN event is 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.
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 ).

The pointer id can be used to track the finger. The pointer id takes effect from the moment it is pressed until it becomes invalid.
If an ACTION_DOWN event is intercepted by the parent View, no Touch event will be received by any child View (this meets the requirements ).
If a non-ACTION_DOWN event is intercepted by the parent View, the child views that last processed 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.

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 ).
If the parent View intercepts the event in onInterceptTouchEvent, The onInterceptTouchEvent will no longer receive the Touch event, and the event will be handled by itself (according to the normal View processing method ).
The following layers describe some details.

1. Activity layer:Copy codeThe Code is as follows: public boolean dispatchTouchEvent (MotionEvent ev ){
If (ev. getAction () = MotionEvent. ACTION_DOWN ){
OnUserInteraction ();
}
If (getWindow (). superDispatchTouchEvent (ev) {// submit it to the View layer for processing.
Return true;
}
Return onTouchEvent (ev); // if the View layer is not processed, it will be processed here
}

2. View layer:Copy codeThe Code is as follows: public boolean dispatchTouchEvent (MotionEvent event ){
// Some details are omitted.
ListenerInfo li = mListenerInfo;
If (li! = Null & li. mOnTouchListener! = Null & (mViewFlags & ENABLED_MASK) = ENABLED
& Amp; li. mOnTouchListener. onTouch (this, event )){
Return true;
}
If (onTouchEvent (event )){
Return true;
}
Return false;
}

The onTouch method of View has a lot of code. The main logic is divided into two steps: first, the event is handed over to TouchDelegate for processing (if any), and if TouchDelegate is not processed, it will be processed by itself; self-processing is mainly responsible for changing the View status (such as pressing the status), long-pressed events, and detection and triggering of click events.

3. ViewGroup layer (more complex ):
The overall logic for the ViewGroup layer to process Touch events is to first check whether interception is required. If no interception is required, it is sent to the subview for processing. If the subview is not processed, it is processed by itself, the logic of Self-processing is the same as that of View.
The logic of interception is to regard all events from down to up as a group of events. If they are intercepted from down, all other events in the group will be handled by you, you do not need to enter the interception logic any more. If you want to intercept events from the middle, you must first send the cancel event to the sub-View. Other events in the group are completely handled by you, and you do not need to enter the interception logic any more.

The distribution logic is that when the ACTION_DOWN event is performed, the sub-View is called the Target. If the Target is not found, the sub-View is processed by itself. If the Target is found, the sub-View is handled by the Target.
From the code point of view, dispatchTouchEvent is responsible for the distribution logic, and onTouchEvent is responsible for the real processing logic. Generally, onTouchEvent should be reloaded. only in special circumstances must dispatchTouchEvent be reloaded.

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.