A. Preschool base (understanding Activity, Window, DecorView, ViewRoot) :

1. Activity:

Activities only control the life cycle and process events, not view control. PhoneWindow really controls the view. An Activity contains a PhoneWindow, which really represents a window. An Activity is like a controller,Coordinate the addition and display of views and interact with the PhoneWindow and View through other callback methods.

2. PhoneWindow:

A PhoneWindow is a view bearer that holds a DecorView inside, and that DecorView is the root layout of the view. PhoneWindow is a subclass of the only implementation of Window. PhoneWindow has an internal class, DecorView, that creates a DecorView to load the layout r.layout.activity_main set in the Activity. PhoneWindow loads the DecorView through WindowManager and passes the DecorView to ViewRoot for view drawing and other interactions.

3. DecorView:

DecorView is a subclass of FrameLayout, which is considered the root view of the Android view tree. DecorView, as a top-level View, normally contains a vertical LinearLayout inside the LinearLayout. Inside the LinearLayout there are three parts, a ViewStub, lazy loading View. In the middle is the title bar (depending on the Theme setting, some layouts do not), and below is the content bar. The specific situation depends on the Android version and body. For example, one of the layouts is as follows:

<FrameLayout style="? android:attr/windowTitleBackgroundStyle" android:layout_width="match_parent" android:layout_height="? android:attr/windowTitleSize"> <TextView android:id="@android:id/title" style="? android:attr/windowTitleStyle" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:fadingEdge="horizontal" android:gravity="center_vertical" /> </FrameLayout> <FrameLayout android:id="@android:id/content" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:foreground="? android:attr/windowContentOverlay" android:foregroundGravity="fill_horizontal|top" />Copy the code
ViewGroup content = (ViewGroup)findViewById(android.R.id.content);
ViewGroup rootView = (ViewGroup) content.getChildAt(0);
Copy the code

4. ViewRoot

All interactions such as View drawing and event distribution are performed or delivered through it.

ViewRoot corresponds to the ViewRootImpl class, which is the link between Windows ManagerService and the DecorView. The View’s three processes (measure, layout, and draw) are all done through ViewRoot.

ViewRoot is not part of the View tree. From the source implementation, it is neither a subclass of View nor a parent class of View, but it implements the ViewParent interface, which allows it to act as the nominal parent of the View. ViewRoot inherits the Handler class, which can receive and distribute events. All Android touch screen events, keystroke events, interface refresh events are distributed through ViewRoot.

5. To summarize

In summary, an Activity is like a controller, not responsible for the view part. The Window acts as a bearer for the internal view. A DecorView is a top-level View, the outermost layout of all views. ViewRoot acts as a connector for communication, notifying views through hardware awareness, and interacting with users.

When the user clicks on the screen, a touch is captured by the underlying hardware, passed to the ViewRootImpl, and passed to the DecorView, which in turn passes to the PhoneWindow, which in turn passes to the Activity. And then there’s the View event distribution that we see all the time.

The event distribution process is as follows:

Hardware -> Viewrotimpl -> DecorView -> PhoneWindow -> Activity- >ViewGroup- >View

Ii. Event distribution mechanism

1. What was the event?

When the user touches the screen (View or ViewGroup control), a series of Touch events are generated, starting with DOWN events, ending with UP events, and consisting of countless MOVE events in the middle.

As shown below:

2. Nature of event Distribution:

The details of Touch events (such as location, time, etc.) are encapsulated into MotionEvent objects. The essence of distribution is the whole process of passing a MotionEvent to a specific View and processing it.

3. Between which objects are events passed?

Activity–>ViewGroup–>View

4. What are the methods of event distribution?

DispatchTouchEvent (): dispatches the pass event, called when the touch event is passed to the current View;

OnInterceptTouchEvent (): Checks whether an event has been intercepted. This method exists only in the ViewGroup and is called within the ViewGroup’s dispatchTouchEvent();

OnTouchEvent (): Handles touch events, called within dispatchTouchEvent();

5. The flow of Activity event distribution mechanism is as follows:

6. The process of ViewGroup event distribution mechanism is as follows:

7. The process of View event distribution mechanism is shown as follows:

3. To summarize

The paper come zhongjue shallow, must know this to practice;

Refer to the flow chart to see the source code several times, deepen understanding and more practice is OK!