第四章-View的工作原理(MeasureSpec、LayoutParams)

主要介绍一下View的工作原理,还有自定义View的实现方法,在Android知识体系中,View是一个很重要的角色,简单来说,View是Android中视觉的呈现。在界面上Android提供了一套完整的GUI库,里面有很多控件,但是有时候往往并不能满足于需求,所以只有自定义View了。
除了View的三大流程之外,View常见的回调方法也是必须掌握的,比如构造方法,onAttach,onVisibilityChanged,onDetach,另外对于一些有滑动效果的自定义View,还要处理滑动事件和滑动冲突,总的来说,自定义View有几种固定的类型,View或者ViewGroup,有的直接重写原生控件。

1、初识ViewRoot和DecorView
在正式介绍View的三大流程之前,我们还是要了解一些基本的概念,本节主要介绍ViewRoot和DecorView的概念。

ViewRoot对应于ViewRootImpl类,他是连接WindowManager和DecorView的纽带,View的三大流程都是通过ViewRoot来完成的,在ActivityThread中,当Activity被创建完毕后,会将DecorView添加到Window中,同时会创建ViewRootImpl对象,并将ViewRootImpl对象和DecorView建立联系,这个可以参照如下源码:

root = new ViewRootImpl(view.getContext(),display);
root.setView(view,wparams,panelParentView);

View的绘制流程从ViewRoot的perfromTraversals方法开始,它经过measure,layout和draw三个过程才能最终将View绘制出来,其中measure测量view的宽和高,layout确定view在父容器的位置,draw负责将view绘制在屏幕上,针对perfromTraversals的大致流程,可以用如下图表示:
第四章-View的工作原理(MeasureSpec、LayoutParams)_第1张图片
第四章-View的工作原理(MeasureSpec、LayoutParams)_第2张图片

第四章-View的工作原理(MeasureSpec、LayoutParams)_第3张图片

2、理解MeasureSpec
第四章-View的工作原理(MeasureSpec、LayoutParams)_第4张图片

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(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
								  @MeasureSpecMode int mode) {
	if (sUseBrokenMakeMeasureSpec) {
		return size + mode;
	} else {
		return (size & ~MODE_MASK) | (mode & MODE_MASK);
	}
}

public static int getMode(int measureSpec) {
	//noinspection ResourceType
	return (measureSpec & MODE_MASK);
}

public static int getSize(int measureSpec) {
	return (measureSpec & ~MODE_MASK);
}

第四章-View的工作原理(MeasureSpec、LayoutParams)_第5张图片

  • UNSPECIFIED

父容器不对View有任何的限制,要多大给多大,这种情况一般用于系统内部,表示一种测量的状态

  • EXACTLY

父容器已经检测出View所需要的精度大小,这个时候View的最终大小就是SpecSize所指定的值,它对应于LayoutParams中的match_parent和具体的数值这两种模式

  • AT_MOST

父容器指定了一个可用大小,即SpecSize,view的大小不能大于这个值,具体是什么值要看不同view的具体实现。它对应于LayoutParams中wrap_content。

3、MeasureSpec 和 LayoutParams 的对应关系

第四章-View的工作原理(MeasureSpec、LayoutParams)_第6张图片
第四章-View的工作原理(MeasureSpec、LayoutParams)_第7张图片

下面看下DecorView的MeasureSpec创建过程:

对于DecorView来说,在ViewRootImpl中的measureHierarchy方法中有这么一段代码。他展示了DecorViwew的MeasureSpec创建过程,其中desiredWindowWidth和desiredWindowHeight是屏幕的尺寸

childWidthMeasureSpec = getRootMeasureSpec(baseSize, 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的MesourSpec的产生过程就很明确了,具体来说其遵守了如下格式,根据layoutparams的宽/高的参数来划分

  • LayouParams.MATCH_PARENT:精确模式,大小就是窗口的大小;
  • LayouParams.WRAP_CONTENT:最大模式,大小不定,但是不能超出屏幕的大小;
  • 固定大小(比如100dp):精确模式,大小为LayoutParams中指定的大小。

下面看下普通View的MeasureSpec创建过程:

对于普通的View来说,这里是指我们布局中的View,View的measure过程由ViewGroup传递而来,先看下ViewGroup的measureChildWithMargis方法

protected void measureChildWithMargins(View child,
	int parentWidthMeasureSpec, int widthUsed,
	int parentHeightMeasureSpec, int heightUsed) {
	final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

	final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
			mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
					+ widthUsed, lp.width);
	final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
			mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
					+ heightUsed, lp.height);

	child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

第四章-View的工作原理(MeasureSpec、LayoutParams)_第8张图片

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 him 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);
}

第四章-View的工作原理(MeasureSpec、LayoutParams)_第9张图片
第四章-View的工作原理(MeasureSpec、LayoutParams)_第10张图片

你可能感兴趣的:(Android艺术开发阅读)