一、简单背景介绍
在自定义view(不是ViewGroup)时,需要重写父类的onMeausre(), onLayout(), onDraw():
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
看了很多书和文章,都介绍了widthMeasureSpec是什么东西。他是一个MeasureSpec
,32位的int,前两位记录了他的mode,后30位记录了他的size。
mode如下:
Mode | Name | Cool |
---|---|---|
00 | UNSPECIFIED | 0 |
01 | EXACTLY | 1073741824 |
10 | AT_MOST | -2147483648 |
但是widthMeasureSpec和heightMeasureSpec是从哪儿来的?他的mode和size是在xml中定义的mode和size?还是他父亲的mode和size?
二、探索onMeasure中的widthMeasureSpec
自定义view的xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<com.example.wangxuan.customviewheight.CustomView
android:layout_width="300px"
android:layout_height="400px"
android:background="@color/colorPrimary" />
</RelativeLayout>
自定义view的java代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
Log.e(TAG, "before wspec:" + widthSpec + ",w:" + width + ",hspec:" + heightSpec + ",h:" + height);
if (widthSpec == MeasureSpec.EXACTLY) {
Log.e(TAG, "width EXACTLY");
} else if (widthSpec == MeasureSpec.AT_MOST) {
Log.e(TAG, "width AT_MOST");
} else {
Log.e(TAG, "width UNSPECIFIED");
}
if (heightSpec == MeasureSpec.EXACTLY) {
Log.e(TAG, "height EXACTLY");
} else if (heightSpec == MeasureSpec.AT_MOST) {
Log.e(TAG, "height AT_MOST");
} else {
Log.e(TAG, "height UNSPECIFIED");
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.e(TAG, "after wspec:" + widthSpec + ",w:" + width + ",hspec:" + heightSpec + ",h:" + height);
if (widthSpec == MeasureSpec.EXACTLY) {
Log.e(TAG, "width EXACTLY");
} else if (widthSpec == MeasureSpec.AT_MOST) {
Log.e(TAG, "width AT_MOST");
} else {
Log.e(TAG, "width UNSPECIFIED");
}
if (heightSpec == MeasureSpec.EXACTLY) {
Log.e(TAG, "height EXACTLY");
} else if (heightSpec == MeasureSpec.AT_MOST) {
Log.e(TAG, "height AT_MOST");
} else {
Log.e(TAG, "height UNSPECIFIED");
}
}
运行结果:
E/CustomView: before wspec:1073741824,w:300,hspec:-2147483648,h:1038
E/CustomView: width EXACTLY
E/CustomView: height AT_MOST
E/CustomView: after wspec:1073741824,w:300,hspec:-2147483648,h:1038
E/CustomView: width EXACTLY
E/CustomView: height AT_MOST
E/CustomView: before wspec:1073741824,w:300,hspec:1073741824,h:400
E/CustomView: width EXACTLY
E/CustomView: height EXACTLY
E/CustomView: after wspec:1073741824,w:300,hspec:1073741824,h:400
E/CustomView: width EXACTLY
E/CustomView: height EXACTLY
三、解释
在RelativeLayout中的onMeasure中会对其所有的子view进行测量。因为RelativeLayout的水平和竖直方向都是相对的,所以在进行测量其子view的大小时,需要分水平和竖直两个方向分别测量,即先测量width,在测量height。下面只给出了测量width的源代码:
int count = views.length;
for (int i = 0; i < count; i++) {
View child = views[i];
if (child.getVisibility() != GONE) {
LayoutParams params = (LayoutParams) child.getLayoutParams();
int[] rules = params.getRules(layoutDirection);
applyHorizontalSizeRules(params, myWidth, rules);
measureChildHorizontal(child, params, myWidth, myHeight);
if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) {
offsetHorizontalAxis = true;
}
}
}
可以看到,RelativeLayout在测量子view的时候,是调用了child.getLayoutParams()
。而myWidth和myHeight则是RelativeLayout的width和height。所以可知自定义view的widthMeasureSpec是自身xml规定的size和mode。那传进去的父view的myWidth和myHeight实际是为了帮助子 view进行一些测量,比如match_parent时,需要通过父亲的大小来界定子 view的大小。