UI 绘制流程及原理
- Window 是一个抽象类,提供了绘制窗口的一组通用 API。
- PhoneWindow 是 Window 唯一的、具体的继承实现类,而且该类包含了一个 DecorView 对象,该 DecorView 对象是所有应用窗口(Activity 界面)的根 View。
- DecorView 是 PhoneWindow 的内部类,是 FrameLayout 的子类,是对 FrameLayout 进行功能的修饰(所以叫 DecorXXX),是所有窗口的根 View。
This is an optimized layout for a screen, with the minimum set of features
enabled.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ViewStub android:id="@+id/action_mode_bar_stub"
android:inflatedId="@+id/action_mode_bar"
android:layout="@layout/action_mode_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="?attr/actionBarTheme" />
<FrameLayout
android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundInsidePadding="false"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>
View 是如何被添加到屏幕窗口上的
- 创建顶层布局容器 DecorView
- 在顶层布局中加载基础布局 ViewGroup
- 将 ContentView 添加到基础布局中的 FrameLayout 中
View 的绘制流程
- 绘制入口
ActivityThread.handleResumeActivity
--> WindowManagerImpl.addView(dercorView, layoutParams)
--> WindowManagerGlobal.addView()
- 绘制的类及方法
ViewRootImpl.setView(dercorView, layoutParams, parentView)
--> ViewRootImpl.requestLayout() --> scheduleTraversals() --> doTraversal()
--> performTraversals()
- 绘制三大步骤(测量 -> 布局 -> 绘制)
测量:ViewRootImpl.performMeasure
布局:ViewRootImpl.perforLayout
绘制:ViewRootImpl.perforDraw
DecorView 被添加到窗口的过程
ActivityThread(handlerResumeActivity()) -> WindowManagerImpl(addView()) -> WindowManagerGlobal(addView())
-> ViewRootImpl(addView()) -> ViewRootImpl(requestLayout()) -> ViewRootImpl(scheduleTraversals())
-> ViewRootImpl(doTrvaersal()) -> ViewRootImpl(performTraversals()) -> 进入 View 的绘制流程
本文由
A lonely cat
原创发布于
阳光沙滩
,未经作者授权,禁止转载