preface

1. Content of inquiry
  • In the last article “Step by step to explore and learn the Android Touch event delivery mechanism (1)”, the ACTION_DOWN event has been taken as an example to explore the Touch event delivery mechanism of the Android system, and the conclusion of good image memory has been drawn.

  • This article explores the issues closely related to the last article. If you are not familiar with the Android Touch event distribution mechanism, you are advised to read the last article “Step by step to learn the Android Touch event distribution mechanism (1)”.

  • We know that an action, such as a click event, is made up of several motion_events of different types. For example, the click event consists of an ACTION_DOWN event and an ACTION_UP event. Do ACTION_UP and ACTION_MOVE events follow the same distribution rules as ACTION_DOWN events?

  • In fact, the answer is no. The flow of ACTION_UP and ACTION_MOVE events is closely related to how and where ACTION_DOWN events are delivered up to this point. Specific we explore the following step by step understanding.

2. Methods of inquiry
  • We still continue the above article “step by step to explore learning Android Touch event distribution transfer mechanism (a)” in the Demo as an example, step by step to hit the Log, to explore the whole event transfer process, and then use the drawing to describe his mechanism.

  • Of course, know why, for Android source analysis, will be the next article “step by step to explore learning Android Touch event distribution transmission mechanism (three)” in the analysis.


We’ll see in the Demo

1. The Demo code
  • The Demo code, both interface and Java code, remains the same as last time.

  • Android Touch Event Delivery Mechanism (part 1)

2. Log to find patterns and identify mechanisms

1.) All methods(dispatchTouchEvent (); onInterceptTouchEvent(); OnTouchEvent (),Return super:

  • First, we keep all methods return super. Of course, I explored this in my last article. ACTION_DOWN events are distributed through the View tree in a U-like route.

  • What about ACTION_UP and ACTION_MOVE events? The ACTION_UP event is taken as an example to explore. (In fact, Action_move events follow a similar pattern to Action_up events.) I perform a click on the View in the Demo screen (as mentioned earlier, the click event is composed of an ACTION_DOWN event and an ACTION_UP event).

  • Play the log:

    Write the picture description here

    Note: In the figure above, the purple box is the ACTION_DOWN distribution process of click events; In the green box is the ACTION_UP event distribution flow for the click event. And the same thing happens in the next diagram.

  • Rule: As you can see, ACTION_UP events are not passed through the View tree in the u-shaped structure of the class, but are consumed directly in the Activity’s onTouchEvent method.

  • The drawing is as follows:

    Write the picture description here

2.) ViewGroup2dispatchTouchEvent()Return true

  • We then explore how ACTION_UP events are delivered when ACTION_DOWN events are consumed in dispatchTouchEvent ().

  • Make ViewGroup2 dispatchTouchEvent() return true, type log:

    Write the picture description here

  • Rule: When an ACTION_DOWN event is consumed in a View or ViewGroup’s dispatchTouchEvent() method, the corresponding ACTION_UP event is also consumed, terminating transmission.

  • To plot:

    Write the picture description here

3.) ViewGroup2OnInterceptTouchEvent () and onTouchEvent ()Return true

  • That is, let ViewGroup2 intercept the event and consume it itself.

    Write the picture description here

  • Play the log:

    Write the picture description here

  • Rule: As you can see, when an ACTION_DOWN event is consumed in the onTouchEvent() method of a View or ViewGroup, the corresponding ACTION_UP event is also consumed there, terminating delivery.

    It is worth noting, however, that the ACTION_UP event no longer passes through the onInterceptTouchEvent() method in this case.

  • The drawing is as follows:

    Write the picture description here

  • 4.) ViewGroup2onTouchEvent()Return true

  • VIewGroup2’s onTouchEvent() method consumes the ACTION_DOWN event it receives from the View’s onTouchEvent() method. Look at how the ACTION_UP event is passed in this case.

  • Play the Log:

    Write the picture description here

  • Rule: As you can see from Log, when an ACTION_DOWN event is consumed by a control’s onToucEvent() method, its corresponding ACTION_UP event is only passed to that control. That is, it is not passed to controls deeper than this control.

  • Drawing a graph:

    Write the picture description here


summarizing

  • ACTION_MOVE events and ACTION_UP events follow similar rules. The above analysis only takes ACTION_UP as an example.

  • The delivery and distribution of ACTION_MOVE events and ACTION_UP events are closely related to previous ACTION_DOWN events.

  • Specifically, when ACTION_DOWN events are consumed in the dispatchTouchEvent() method, the corresponding ACTION_MOVE and ACTION_UP events are also passed down to the control’s method and consumed.

  • When an ACTION_DOWN event is consumed in the onTouchEvent() method, the corresponding ACTION_MOVE and ACTION_UP events are only passed to the onTouchEvent() method on the control and then consumed and terminated. Passes to the controls under the control are not experienced.

  • When all methods default to return super, ACTION_MOVE events and ACTION_UP events are consumed in the Activity’s onTouchEvent().

Note: [reproduced please specify, questions can be asked, like can collect to share, the blog continues to update, welcome to pay attention to]