入口
ViewTree开始绘制view的工作入口是ViewRootImpl的performTraveals方法。performTraveals方法中又主要调用了3个方法performMeasure,performLayout,performDraw。
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
try {
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);//又会调用onMeasure,在onMeasure中完成对子元素的measure过程
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
performLayout和performDraw类似。
2018-10-17_165715.png
测量 onMeasure
MeasureSpec可以理解成是测量规格,是一个32位的int值 高2位代表测量模式SpecMode,低30位代表规格大小SpecSize
SpecMode分为3类 表示了viewgroup对view的态度
-
UNSPECIFIED = 0<<30
没有限制,要多大给多大
Measure specification mode: The parent has not imposed any constraint
on the child. It can be whatever size it wants. -
Exactly = 1<<30
The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
父容器已经为子View确认了一个具体的尺寸。不管孩子有多大,孩子们都会被给予这些限制。对应于具体数值或者match_parent -
AT_MOST = 2 <<30
The child can be as large as it wants up to the specified size.
指定了一个可用大小Specsize,view不能超过这个值
系统会将LayoutParams,即layout_width,layout_height在父容器的约束下转换成对应的MeasureSpec,从而确定View的测量后的寬高。所以,仔细一想在LayoutInflater解析xml布局的时候,除了layoutId,还会要求第二个参数父容器ViewGroup。如果传入null,那就是一个缺少大小的view。
View的测量过程 是通过ViewGroup的measureChildWithMargins
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//child 获得自己的layoutparams
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);
}
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);
}
从getChildMeasureSpec可以看出这个parent的measureSpec和子元素的layoutParams共同生成了子元素的measureSpec。可以得到这样一个规则表
getChildMeasureSpec.png
- child是固定dp的,那么child的大小就是这固定的值
- view宽高是match_parent,如果父容器是精准模式Exactly,那么view也是精准模式,大小为parent的剩余空间。如果是AT_MOST,那么view也是AT_MOST,大小不超过parent的剩余空间
- view宽高是wrap_content,view总是最大化AT_MOST,并且不超过父容器的剩余空间
比如说如下CustomView只是继承自view,什么都没添加,那么肯定是占满整个屏幕。而如果是TextView则肯定不会,因为TextView在onMeasure肯定在AT_MOST的情况下修改了宽高值。所以自定义View的时候,需要在模式为AT_MOST的情况,告诉viewgroup自身的大小
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.administrator.viewdemo.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
</android.support.constraint.ConstraintLayout>
那么动手就简单的自定义一个View继承ImageView,让它是一个正方形的。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int width = getMeasuredWidth();//得到计算的值
int height = width;
setMeasuredDimension(width,height);//重新保存尺寸值
}
而如果是全新定义View(比如自己实现imageview)的尺寸时,需要自己去计算view的大小,然后通过resolveSize,考虑下父控件给的尺寸大小,合成下结果,最后再保存
public static int resolveSize(int size, int measureSpec) {
return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
}
/**
* Utility to reconcile a desired size and state, with constraints imposed
* by a MeasureSpec. Will take the desired size, unless a different size
* is imposed by the constraints. The returned value is a compound integer,
* with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
* optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the
* resulting size is smaller than the size the view wants to be.
*
* @param size How big the view wants to be.
* @param measureSpec Constraints imposed by the parent.
* @param childMeasuredState Size information bit mask for the view's
* children.
* @return Size information bit mask as defined by
* {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
*/
public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
final int specMode = MeasureSpec.getMode(measureSpec);
final int specSize = MeasureSpec.getSize(measureSpec);
final int result;
switch (specMode) {
case MeasureSpec.AT_MOST:
if (specSize < size) {
//当specMode为AT_MOST,并且父控件指定的尺寸specSize小于View自己想要的尺寸时,
//我们就会用掩码MEASURED_STATE_TOO_SMALL向量算结果加入尺寸太小的标记
//这样其父ViewGroup就可以通过该标记其给子View的尺寸太小了,
//然后可能分配更大一点的尺寸给子View
result = specSize | MEASURED_STATE_TOO_SMALL;
} else {
result = size;
}
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
case MeasureSpec.UNSPECIFIED:
default:
result = size;
}
return result | (childMeasuredState & MEASURED_STATE_MASK);//还会返回state
}
自定义ViewGroup,则测量过程是遍历让子元素去调用measure方法。可以参考FrameLayout的onMeasure方法
布局onLayout
ViewGroup摆放view的方法。内部自己的逻辑主要是遍历所有childview,然后让child调用layout方法。
绘制draw
绘制的过程包括下面几个步骤
- 绘制背景 drawBackground(canvas)
- 绘制自己 onDraw(canvas) 对View的内容进行绘制
- 绘制children dispatchDraw(canvas) 当前View的所有子View进行绘制,如果当前的View没有子View就不需要进行绘制
- 绘制装饰 onDrawScrollBars(canvas) 对View的滚动条进行绘制
- 绘制前景 onDrawForeground() API 23 才引入的,所以在重写这个方法的时候要确认你的 minSdk 达到了 23,不然低版本的手机装上你的软件会没有效果。
如何去绘制,是一个比较大的学问。主要是paint,path,canvas,pathMeasure等API的调用,可以去学习GcsSloop的自定义View教程。还有绘制的时候是绘制的顺序也有讲究,详细讲解:HenCoder Android 自定义 View 1-5: 绘制顺序,主要要想清楚绘制的内容是在super的上面还是下面。另外onDraw的时候,比如有些动态的,可能会不断invalidate的,这个时候paint类的初始化要注意,不要在ondraw方法内,会产生构造很多的对象。
硬件加速
看了HenCoder Android 自定义 View 1-8 硬件加速文章后,
总结硬件加速更快的原因有两条:
- 工作分摊给了 GPU,绘制变快了;
- 绘制机制的优化,导致界面内容改变时的刷新效率极大提高。刷新的时候不用再从下往上所有都重绘
网友评论