Read The Fucking Source Code

The introduction

What is the drawing process for Android? OnMeasure, onLayout, onDraw?? This is already drawing the distribution process. Now explore the origins of the drawing process

Android Q — API 29

[Android App startup process]

1. An overview

  • Everyone knows that the drawing is distributed from ViewRootImpl, and the DecorView does the drawing distribution.
  • So where does a DecorView come from?
  • Where did the ViewRootImpl come from?
  • After the application starts, at what point does it draw?
  • With these questions in mind, let’s explore the origins of the drawing process

2. The plane analysis

2.1 create PhoneWindow

2.2 create a DecorView

2.3 add DecorView

2.4 create ViewRootImpl

2.5 Adding a View to the WMS

2.6 The Vsync clock triggers the drawing process

3. Summary

4. Think about

What is the relationship between ViewRootImpl and DecorView?

  • On Android, a Window is the carrier of a View. Under each Window there is a View tree. The top View of each Window is a DecorView.
  • The ViewParent specifies what the parent View is supposed to do, and the ViewGroup implements this interface. The ViewGroup is the object that wraps the View.
  • The ViewRootImpl implements ViewParent and takes over the ViewParent responsibilities of the DecorView, handling all external work of the View (such as the refresh clock /input input).
  • View Rotimpl can be understood as View’s foreign minister. Because ViewRootImpl is not really a View, it just takes over the ViewParent duties of the DecorView, and it is not accurate to say that ViewRootImpl is the parent of the DecorView. Should be DecorView’s goddad.

View drawing process mainly refers to measure, layout, draw. Give a brief description of your responsibilities.

  • Measure Determines the measurement width/height of the View
  • Layout determines the final width/height of the View and the position of the four vertices
  • Draw draws the View to the screen

When was the drawing process of the first View triggered?

  • Trigger ActivityThread. HandleResumeActivity.
  • Finally through WindowManagerImpl. AddView – > WindowManagerGlobal. AddView – > ViewRootImpl. SetView – > ViewRootImpl. RequestLayout That triggers the first View drawing.

What is the layout of the DecorView?

  • For the Activity hierarchy, Activity -> PhoneWindow -> DecorView -> [title_bar, content], The title_bar and Content views are included in the DecorView, but this is the default layout. In fact, depending on the theme style, the DecorView should have a different layout.
  • DecorView default layout is r.layout.screen_simple. What is the layout of the DecorView based on? The theme…

Activity, PhoneWindow, DecorView, ViewRootImpl?

  • PhoneWindow is actually the only subclass of Window, which is the middle layer of the Activity and View interaction system. DecorView is the top layer of the View hierarchy. ViewRootImpl is the parent of DecorView. It is not a real View, but inherits the ViewParent interface that handles all View events, including requestLayout, Invalidate, dispatchInputEvent, and so on.

Why is the measurement width not available during the Activity lifecycle? Is there any way to solve this problem?

  • Because the View’s measurement process has nothing to do with the Activity’s life cycle. Because it starts after the end of the life cycle.
  • Solution 1: The View creates an array of handlerActions (including Runnable) of length 4 before it has even started distributing the measure drawing. When ViewRootImpl calls the AttchInfo object’s dispatchAttachedToWindow() method, the cache Runnable from the HandlerActionQueue is added to the ViewRootImpl Handler. Because the draw refresh event uses the Handler’s synchronization barrier (i.e., high-priority messages), the cache Runnable that was added will have to wait until the measure is drawn, so it is normal to fetch the measure width later in the Runnable process.
  • Scheme 2: by setting the ViewTreeObserver OnDrawListener for access (example: mRootView. GetViewTreeObserver () addOnDrawListener (omitted)). Because ViewRootImpl will call after performMeasure, performLayout mAttchInfo. MTreeObserver. DispatchOnPreDraw method. So it can be monitored.

Xiaobian extension links

Android View Module Family Bucket