1. 初识ViewRoot 和 DecorView
自定义View (控件) 可以实现各种效果
1. ViewRoot
对应于 ViewRootImpl 类,是连接 WindowManager 和 DecorView 的纽带, View 的三大流程都是通过 ViewRoot 来完成的。
在 ActivityThread 中,当 Activity 对象被创建完毕后,会将 DecorView 添加到 Window 中,同时创建 ViewRootImpl 对象,并将 ViewRootImpl对象和 DecorView 建立关联。
root = new ViewRootImpl(view.getContext(), display);
root.setView(view, wparams,panelParenrView);
View 的绘制流程是从 ViewRoot 的 performTraversals 方法开始的,经过 measure、layout、draw 三个过程才能最终将一个View绘制出来。
measure :测量 View 的宽和高
layout: 用来确定View 在父容器中的放置位置
draw :负责将 View 绘制在屏幕上
performTraversals 会依次调用 performMeasure 、 performLayout 、performDraw 三个方法,完成顶级View 的measure、layout 和draw流程。
其中performMeasure 会调用measure ——> onmeasure ,对所有的子元素进行 measure, 这个时候 measure 流程就从父容器传递到子元素了,完成了一次 measure 过程。接着子元素会重复父容器的 measure 过程,如此反复就完成了整个 View 树的遍历。Layout 和 draw 也类似。但draw 的传递是通过dispatchDraw来实现的,没有本质区别。
measure 的过程决定了 View 的 宽/高,measure 完成以后可以通过 getMeasureWidth 和 getMeasureHeight 方法来获取到 View 测量之后的 宽 / 高。 几乎所有的情况下,它都等同于View 最终的宽/高,特殊情况除外。
Layout 过程决定了View 的四个顶点的坐标和实际的 View 的宽/高, 完成以后,可以通过getTop、getBottom、getLeft、getRight 来拿到View 四个顶点的位置,并可以通过getWidth 和 getHeight 方法来拿到 View 的最终宽/高。
Draw 过程决定了View的显示,只有 draw 方法完成以后 View 的内容才能显示在屏幕上。
DecorView 作为顶级View , 一般情况下它内部会包含一个竖直方向的 LinearLayout
这个LinearLayout 里面有上下两个部分(上面是标题栏,下面是内容栏)
在 Activity 中我们通过 setContentView 所设置的布局文件其实就是被加到内容栏之中的,而内容栏的id 是 content ,布局加到了 id 为content的FrameLayout中。
如何得到 content?
ViewGroup content = findViewById(ViewGroup)findViewById(android.R.id.content)
如何得到我们设置的 view 呢?
content.getChildAt(0)
DecorView 其实是一个 FrameLayout , View 层的事件都先经过DecorView,然后才传递给我们的View
顶级View: DecorView 的结构
2. 理解 MeasureSpec
"测量规格"
在很大程度上决定了一个 View 的尺寸规格,这个过程还受父容器的影响。
因为父容器影响 View 的 MeasureSpec 的创建过程。
在测量过程中,系统会将View 的LayoutParams 根据父容器所施加的规则转换成对应的 MeasureSpec
然后根据这个 measureSpec 来测量出 View 的宽/高,但不一定是最后的宽/高。
2.1. MeasureSpec
MeasureSpec 代表一个32位int 值,高2位代表SpecMode,低30位代表 SpecSize , SpecMode 是指测量模式, Spec 是指在某种测量模式下的规格大小。
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
public static final int EXACTLY = 1 << MODE_SHIFT;
public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec( int size,int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
MeasureSpec 通过将 SpecMode 和 SpecSize 打包成一个int 值来避免过多的对象内存分配。
为了方便操作,其提供了打包和解包方法。
SpecMode 和 SpecSize 也是一个int 值 ,一组 SpecModew 和 SpecSize 可以打包为一个MeasureSpec,而一个MeasureSpec可以通过解包的形式来得出其原始的SpecMode 和 SpecSize ( 这里的JMeasureSpec 指所代表的int值),需要注意的是这里提到的MeasureSpec是指MeasureSpec所代表的int值,而并非MeasureSpec本身。
SpecMode有三类(测量模式),每一类都表示特殊的含义,如下所示。
-
UNSPECIFIED
父容器不对View有任何限制,要多大给多大,这种情况一般用于系统内部,表示一种测量的状态。
-
EXACTLY
父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize所指定的值。它对应于LayoutParams中的match_parent和具体的数值这两种模式。
-
AT_MOST
父容器指定了一个可用大小即SpecSize, View的大小不能大于这个值,具体是什么值要看不同View的具体实现。它对应于LayoutParams中的wrap_content。
2.2 MeasureSpec和LayoutParams的对应关系
系统内部是通过 MeasureSpec 来进行 view 的测量,但正常情况下 我们使用View 指定 MeasureSpec。
尽管如此,但是我们可以给View设置LayoutParams。在View测量的时候,系统会将LayoutParams 在父容器的约束下转换成对应的MeasureSpec,然后再根据这个MeasureSpec来确定View测量后的宽/高。
需要注意的是,MeasureSpec不是唯一由LayoutParams决定的,LayoutParams需要和父容器一起才能决定View的MeasureSpec,从而进一步决定View的宽/高。
另外,对于顶级View(即DecorView)和普通View来说,MeasureSpec的转换过程略有不同。对于DecorView,其MeasureSpec由窗口的尺寸和其自身的LayoutParams来共同确定;对于普通View,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同决定,MeasureSpec一旦确定后,onMeasure中就可以确定View的测量宽/高。
2.3 DecorView
对于 DecorView 来说,在ViewRootImpl中的 measureHierarchy方法中有如下一段代码,它展示了DecorView的MeasureSpec的创建过程,其中desiredWindowWidth和desired-WindowHeight是屏幕的尺寸:
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight,lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
接着再看一下 getRootMeasureSpec 方法的实现:
private static int getRootMeasureSpec(int windowSize, int rootDimension) {
int measureSpec;
switch (rootDimension) {
case ViewGroup.LayoutParams.MATCH_PARENT:
// Window can't resize. Force root view to be windowSize.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
break;
case ViewGroup.LayoutParams.WRAP_CONTENT:
// Window can resize. Set max size for root view.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
break;
default:
// Window wants to be an exact size. Force root view to be that size.
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}
通过以上代码, DecorView 的 MeasureSpec 的产生过程就很明确了,具体来说其遵守如下规则,根据它的LayoutParams中的宽/高的参数来划分。
- LayoutParams.MATCH_PARENT:精确模式,大小就是窗口的大小;
- LayoutParams.WRAP_CONTENT:最大模式,大小不定,但是不能超过窗口的大小;
- 固定大小(比如100dp) :精确模式,大小为LayoutParams中指定的大小。
2.4 普通View
对于普通View来说,这里是指我们布局中的View, View的measure过程由ViewGroup传递而来,先看一下ViewGroup的measureChildWithMargins方法:
protected void measureChildWithMargins(View child,
7011 int parentWidthMeasureSpec, int widthUsed,
7012 int parentHeightMeasureSpec, int heightUsed) {
7013 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
7014
7015 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
7016 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
7017 + widthUsed, lp.width);
7018 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
7019 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
7020 + heightUsed, lp.height);
7021
7022 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
7023 }
上述方法会对子元素进行 measure ,在调用子元素的 measure 方法之前会先通过 getChildMeasureSpec 方法来得到子元素的MeasureSpec。
从代码来看,很显然,子元素的MeasureSpec的创建与父容器的 MeasureSpec* *和子元素本身的LayoutParams有关,此外还和View的margin及padding有关,具体情况可以看一下ViewGroup的getChildMeasureSpec方法,如下所示。
frameworks/base/core/java/android/view/ViewGroup.java
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent has imposed a maximum size on us
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.
// Constrain child to not be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to be
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let them have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
//noinspection ResourceType
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
上述方法不难理解,它的主要作用是根据父容器的MeasureSpec同时结合View本身的LayoutParams来确定子元素的MeasureSpec,参数中的padding是指父容器中已占用的空间大小,因此子元素可用的大小为父容器的尺寸减去padding,具体代码如下所示。
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(0, specSize - padding);
getChildMeasureSpec清楚展示了普通View的MeasureSpec的创建规则,为了更清晰地理解getChildMeasureSpec的逻辑,这里提供一个表,表中对getChildMeasureSpec的工作原理进行了梳理。注意,表中的parentSize是指父容器中目前可使用的大小
表为普通的 view 的 MeasureSpec 的创建规则
普通 view ,其MeasureSpec由父容器的MeasureSpec和自身的LayoutParams来共同决定,那么针对不同的父容器和View本身不同的LayoutParams, View就可以有多种MeasureSpec。