[Android] Drawing of View

process

When an Activity enters the visible state and is about to be displayed on the screen, the following operation process will be triggered

onResume 方法

The Activity's lifecycle method onResume() is called, and some preparatory work can be done inside this method, because at this time the Activity is about to be displayed to the user.

激活 Instrumentation

In the onResume method, the Instrumentation instance will be activated, and its callActivityOnResume method will be called to notify the Activity that it is about to enter the Resume state

WindowManager 添加 DecorView

In the callActivityOnResume method, the addView method of WindowManager will be called to add DecorView to the Window. WindowManager is the class responsible for managing windows in the Android system.

创建 ViewRootImpl

When the addView method of WindowManager is called, a ViewRootImpl object is created. ViewRootImpl is the root node of the view hierarchy, responsible for managing and drawing the entire view hierarchy.

设置 DecorView

Set DecorView as the root view of the view hierarchy by calling the setView method of ViewRootImpl.

添加到集合

After adding DecorView to Window, objects such as DecorView, ViewRootImpl and WindowManager.LayoutParams will also be added to the corresponding collection for subsequent use and management.

绘制和显示

With the DecorView added and set up, the ViewRootImpl handles the drawing. It is responsible for measuring the size of each View, laying out the position of each View, and performing actual drawing work after receiving drawing instructions.

Among them, the settings about DecorView

Use method in Activity setContentView to set layout file eg setContentView(R.layout.activity_main). This method will parse the specified layout file into a View and set it as the main view of the current Activity.

The setContentView method of the Activity will getWindow().setContentView()add the parsed View to a named DecorViewcontainer, that is, the decoration view. DecorView is the topmost container of the entire Activity window, used to host all user interface elements.

When parsing the layout file, various objects defined in XML View(such as TextView, ImageView, etc.) and their attributes will be converted into corresponding objects, and then 层级关系constructed according to those in XML. Eventually, these Views will be added to DecorView and become part of the overall interface.

Measure(测量): After the View is added to DecorView, the system will automatically trigger the measurement process. The measurement will determine the space occupied by each View based on information such as the layout properties of the View and the size of the parent container.

Layout(布局): After the measurement is completed, the system will calculate the position and size of each View based on the measurement results, and lay them out to the appropriate position on the screen.

Draw(绘制): After the layout is completed, the system will call the drawing method of each View to perform the actual drawing operation. The drawing process of View generally includes filling the background, drawing content (such as text, pictures, etc.), handling touch events, and so on.

Three important classes for managing and displaying windows

WindowManagerImpl:

It is one of the implementation classes of WindowManager, which is responsible for communicating with the system window service and adding views to specific screens and parent windows. WindowManagerImpl is mainly responsible for managing operations such as displaying, updating, and removing windows, and completes these operations by calling the underlying window management interface of the system.

WindowManagerGlobal:

It is the global manager of WindowManagerImpl, which is responsible for tracking and managing all window information in the whole process. It maintains a collection of windows (mViews), ViewRootImpl collection (mRoots) and WindowManager.LayoutParams collection (mParams) for storing and managing information about all windows.

ViewRootImpl:

It is the root view of each window, responsible for handling window layout, drawing, and event distribution. ViewRootImpl is bound to a specific Window, and the root view of the Window, namely DecorView, can be obtained through ViewRootImpl. It is the executor of WindowManagerGlobal that actually operates the window, and calls specific drawing and event handling methods.

To sum up, WindowManagerImpl yes WindowManager 接口的实现类, responsible for specific window management operations; WindowManagerGlobal yes WindowManagerImpl 的全局管理器, responsible for tracking and managing all window information; and ViewRootImpl yes 每个窗口的根视图, responsible for window layout, drawing and event handling, etc. The three of them realize the function of effectively managing and displaying windows in Android by calling and cooperating with each other.

Must UI refresh be on the main thread?

no

Under normal circumstances, the UI refresh operation should be performed on the main thread. This is because the Android UI framework is not thread-safe, and UI-related operations can only be updated in the main thread (also called the UI thread), such as modifying the properties of the View, calling the drawing method of the View, and so on. This is to ensure that multiple threads do not make modifications to the UI at the same time, thereby avoiding possible concurrency issues and inconsistencies.

The UI refresh is responsible ViewRootImplfor the refresh in ViewRootImpl, no matter whether it is called requestLayout()or called View.invalidate()to refresh the page, checkThread()the method will be called at the end 线程的检测, when the thread is connected ViewRootImpl线程一致时可以进行页面的刷新, and ViewRootImpl is created under the main thread, so there is this refresh in the main thread UI regulations.

However, if there is a need to refresh the UI in the sub-thread, the following two methods can be adopted:

在ViewRootImpl创建之前调用:

By modifying relevant parameters before the ViewRootImpl instance is created, subsequent UI refresh operations can be executed in sub-threads. But this method requires a deep understanding of Android's internal mechanisms, and may lead to unpredictable results, so it is not recommended for official projects.

在需要刷新UI的子线程创建ViewRootImpl:

Create a new ViewRootImpl instance in the child thread and bind it to the Looper of the current thread. Then use the ViewRootImpl instance to perform UI refresh operations. This method is more common, usually through Handler or AsyncTask, etc., to send messages or execute background tasks in sub-threads, and then perform UI refresh operations in the corresponding processing methods.

It should be noted that UI refresh in sub-threads may cause some problems, such as untimely update of view state, thread synchronization, resource consumption, etc. Therefore, when using sub-threads to refresh the UI, it is necessary to carefully consider and deal with related issues reasonably.

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131910218