一. 前言

从上面的内容我们知道,View的绘制流程开始于ViewRootImpl对象的performTraversals。一层一层从顶级View-DecorView的ViewGroup开始,一层一层从ViewGroup至子View遍历测绘。

即自上而下遍历,由父视图到子视图,每一个ViewGroup负责测绘它所有的子视图,而底层的View会负责绘制自身。

绘制的流程 = measure过程、layout过程、draw过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 源码分析:ViewRootImpl.performTraversals()
*/
private void performTraversals() {
// 1. 执行measure流程
// 内部会调用performMeasure()
measureHierarchy(host, lp, res,desiredWindowWidth, desiredWindowHeight);

// 2. 执行layout流程
performLayout(lp, mWidth, mHeight);

// 3. 执行draw流程
performDraw();
}

二. Measure 过程

自定义View-Measure过程

三. Layout过程

自定义View-Layout过程

四. Draw过程

自定义View-Draw过程