说明:本博客为原创,转载请注明出处 CSDN-ANDROID笔记栈
由于作者水平有限,错误在所难免,请见谅,可以留言,本人会及时改正
索引
onMeasure
该方法为 protected级,只有View的子类 才能重载该方法
// 主要用于测量视图及其内容的大小,该方法被View类的measure(int,int)方法调用
// 重载该方法需要注意几点:
// 1.必须调用setMeasuredDimension(int width, int height)来存储测量结果,否则会抛IllegalStateException异常
// 2.参数widthSpec,heightSpec并不是大小值,而是由MeasureSpec类生成的“mode&size”复合值
// 3.参数 widthSpec,heightSpec由父类生成
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
// measure方法才是测量的入口,onMeasure中确定测量值
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
...
onMeasure(int,int);
...
}
onMeasure(int widthMeasureSpec, int heightMeasureSpec)源码如下:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 调用setMeasureDemension()确定测量结果
// getSuggestedMiniumWidth()根据minWidth还有background drawable返回一个最小值
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec));
}
View getDefaultSize(int size, int measureSpec)源码如下:
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
// 通过MeasureSpec类,拆分measureSpec值,获得mode和size值
// 后面会对这两个值做解释
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
//无限制模式
case MeasureSpec.UNSPECIFIED:
result = size;
break;
//至多模式
case MeasureSpec.AT_MOST:
//精确模式
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
ViewGroup
ViewGroup 在onMeasure过程中稍有不同,因为你只有知道了所有的子View的大小,才能知道你自己需要多大。
Linearlayout 中遍历所有子View,如果是横向布局则把所有子View的width相加,如果是纵向布局则把所有子View的height相加(当然还需要考虑Padding,累加的值是不是超过了ParentView允许的值等等)。
// 大致如下
// 具体代码可以参考LinearLayout、FrameLayout源码
public void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
int childCount=getChildCount();
// 遍历所有子View
for(int i=0;i<childCount;i++){
View child=getChildAt(i);
child.measure(childWidthMeasureSpec,childHeightMeasureSpec);
}
}
ViewGroup中定义了几个protected方法用于测量子View
//ViewGroup自己完成所有子View的测量
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
//过滤GONE状态的View
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
protected void measureChild(View child, int parentWidthMeasureSpec,int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
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);
}
// 生成child measureSpec
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);
}
MeasureSpec类
MeasureSpec类中定义了三种模式分别是:UNSPECIFIED(无限制模式)、
EXACTLY(精确模式)、AT_MOST(至多模式)
private static final int MODE_SHIFT = 30; //30位
private static final int MODE_MASK = 0x3 << MODE_SHIFT; //二进制11(低30位为0),左移了30位
public static final int UNSPECIFIED = 0 << MODE_SHIFT; //二进制00(低30位为0)
public static final int EXACTLY = 1 << MODE_SHIFT; //二进制01(低30位为0)
public static final int AT_MOST = 2 << MODE_SHIFT; //二进制11(低30位为0)
UNSPECIFIED模式
ParentView不约束子View,子View想多大就多大
EXACTLY模式
ParentView已经指定一个精确的大小给子View,子View大小已被确定
<TextView android:layout_width="48dp"
android:layout_height="48dp"/>
AT_MOST模式
ParentView指定一个最大值,子View的大小不能超过这个值
// match_parent 会根据ParentView的模式不同而不同
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
引用其他人的图片做一个总结 CDDN 苦咖啡的自留地
获取Mode和Size
MeasureSpec类定义了几个公共静态方法来获取Mode和size值
//java int类型是32位的,MODE_MASK是110...0(共32位),做&操作能取高2位值也就是mode(UNSPECIFIED,EXACTLY,AT_MOST)值
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
//java int类型是32位的,~MODE_MASK是001...1(共32位),做&操作能取低位30位值也就是size值
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
measureSpec可以通过下面的方法生成
// 低30位size值和高2位mode值
public static int makeMeasureSpec(int size,int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
Demo
GitHub地址: GitHub
环境: Windows7+JAVA8
IDE: AndroidStdio2.2.2
compileSdkVersion:24
测试设备:Nexus5(6.0.1)
运行结果截图
自定义View 重载onMeasure方法并输出日志
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
DLDebug.debug(TAG, "onMeasure: ---->");
DLDebug.debug(TAG, "[width] " + "mode:" + ViewHelper.parseMeasureMode(widthMode) + " , size:" + widthSize);
DLDebug.debug(TAG, "[height] " + "mode:" + ViewHelper.parseMeasureMode(heightMode) + " , size:" + heightSize);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
DLDebug.debug(TAG, "[result] " + getMeasuredWidth() + "," + getMeasuredHeight());
DLDebug.debug(TAG, "<----");
}
//XML源码
<?xml version="1.0" encoding="utf-8"?>
<com.neulion.android.dl.customviewdemo.widget.DLFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.neulion.android.dl.customviewdemo.MainActivity">
<com.neulion.android.dl.customviewdemo.widget.DLCustomViewGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffeb3b">
<com.neulion.android.dl.customviewdemo.widget.DLCustomView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#ff0000" />
<com.neulion.android.dl.customviewdemo.widget.DLCustomView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#00ff00" />
<com.neulion.android.dl.customviewdemo.widget.DLCustomView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#0000ff" />
<com.neulion.android.dl.customviewdemo.widget.DLCustomView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#000000" />
</com.neulion.android.dl.customviewdemo.widget.DLCustomViewGroup>
<com.neulion.android.dl.customviewdemo.widget.DLCustomView
android:id="@+id/dl_custom_view_exactly"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="left|center_vertical"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/device_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top|end"
android:minHeight="48dp"
android:maxWidth="240dp"
android:textSize="14dp"
android:textColor="#000000"
tools:ignore="SpUsage" />
</com.neulion.android.dl.customviewdemo.widget.DLFrameLayout>
结果如下:
// onMeasure方法可能会被多次调用!
12-08 14:48:03.489 I/DL_DLFrameLayout: onMeasure: ---->
12-08 14:48:03.489 I/DL_DLFrameLayout: [width] mode:EXACTLY , size:1080(1080是当前ParentView及Activity根View允许其子View的最大值,DLFrameLayout宽高设置的是match_parent/wrap_content返回这个结果)
12-08 14:48:03.489 I/DL_DLFrameLayout: [height] mode:EXACTLY , size:1392(这个值可以看出是屏幕的高-statusBar高度-actionBar高度-navigationBar高度的结果)
12-08 14:48:03.489 I/DL_DLCustomViewGroup: onMeasure: ---->
12-08 14:48:03.489 I/DL_DLCustomViewGroup: [width] mode:EXACTLY size:1080
12-08 14:48:03.489 I/DL_DLCustomViewGroup: [height] mode:AT_MOST size:1392
12-08 14:48:03.489 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.489 D/DL_DLCustomView: [width] mode:EXACTLY , size:144 // DLCustomView中宽高设置的是48dp,返回这个结果
12-08 14:48:03.489 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.490 D/DL_DLCustomView: <----
12-08 14:48:03.490 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.490 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.490 D/DL_DLCustomView: <----
12-08 14:48:03.490 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.490 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.490 D/DL_DLCustomView: <----
12-08 14:48:03.490 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.490 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.490 D/DL_DLCustomView: <----
12-08 14:48:03.490 I/DL_DLCustomViewGroup: [result] 1080,576
12-08 14:48:03.490 I/DL_DLCustomViewGroup: <----
12-08 14:48:03.490 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.490 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.490 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.490 D/DL_DLCustomView: <----
12-08 14:48:03.508 I/DL_DLFrameLayout: [result] 1080,1392
12-08 14:48:03.508 I/DL_DLFrameLayout: <----
12-08 14:48:03.557 I/DL_DLFrameLayout: onMeasure: ---->
12-08 14:48:03.557 I/DL_DLFrameLayout: [width] mode:EXACTLY , size:1080
12-08 14:48:03.557 I/DL_DLFrameLayout: [height] mode:EXACTLY , size:1536
12-08 14:48:03.557 I/DL_DLCustomViewGroup: onMeasure: ---->
12-08 14:48:03.557 I/DL_DLCustomViewGroup: [width] mode:EXACTLY size:1080
12-08 14:48:03.557 I/DL_DLCustomViewGroup: [height] mode:AT_MOST size:1536
12-08 14:48:03.557 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.557 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.557 D/DL_DLCustomView: <----
12-08 14:48:03.557 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.557 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.557 D/DL_DLCustomView: <----
12-08 14:48:03.557 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.557 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.557 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.557 D/DL_DLCustomView: <----
12-08 14:48:03.557 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.557 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.558 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.558 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.558 D/DL_DLCustomView: <----
12-08 14:48:03.558 I/DL_DLCustomViewGroup: [result] 1080,576
12-08 14:48:03.558 I/DL_DLCustomViewGroup: <----
12-08 14:48:03.558 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.558 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.558 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.558 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.558 D/DL_DLCustomView: <----
12-08 14:48:03.558 I/DL_DLFrameLayout: [result] 1080,1536
12-08 14:48:03.558 I/DL_DLFrameLayout: <----
12-08 14:48:03.558 I/DL_DLFrameLayout: onSizeChanged: 1080x1536 , old 0x0
12-08 14:48:03.559 I/DL_DLFrameLayout: onLayout: true [0,0,1080,1536]
12-08 14:48:03.559 I/DL_DLCustomViewGroup: onSizeChanged: 1080x576 , old 0x0
12-08 14:48:03.559 I/DL_DLCustomViewGroup: onLayout: true [0,0,1080,576]
12-08 14:48:03.559 D/DL_DLCustomView: onSizeChanged: 144x144 , old 0x0
12-08 14:48:03.559 D/DL_DLCustomView: onLayout: true [0,0,144,144]
12-08 14:48:03.559 D/DL_DLCustomView: onSizeChanged: 144x144 , old 0x0
12-08 14:48:03.559 D/DL_DLCustomView: onLayout: true [144,144,288,288]
12-08 14:48:03.559 D/DL_DLCustomView: onSizeChanged: 144x144 , old 0x0
12-08 14:48:03.559 D/DL_DLCustomView: onLayout: true [288,288,432,432]
12-08 14:48:03.559 D/DL_DLCustomView: onSizeChanged: 144x144 , old 0x0
12-08 14:48:03.559 D/DL_DLCustomView: onLayout: true [432,432,576,576]
12-08 14:48:03.559 D/DL_DLCustomView: onSizeChanged: 144x144 , old 0x0
12-08 14:48:03.559 D/DL_DLCustomView: onLayout: true [0,696,144,840]
12-08 14:48:03.572 I/DL_DLFrameLayout: onMeasure: ---->
12-08 14:48:03.572 I/DL_DLFrameLayout: [width] mode:EXACTLY , size:1080
12-08 14:48:03.572 I/DL_DLFrameLayout: [height] mode:EXACTLY , size:1536
12-08 14:48:03.572 I/DL_DLCustomViewGroup: onMeasure: ---->
12-08 14:48:03.572 I/DL_DLCustomViewGroup: [width] mode:EXACTLY size:1080
12-08 14:48:03.572 I/DL_DLCustomViewGroup: [height] mode:AT_MOST size:1536
12-08 14:48:03.572 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.572 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.572 D/DL_DLCustomView: <----
12-08 14:48:03.572 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.572 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.572 D/DL_DLCustomView: <----
12-08 14:48:03.572 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.572 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.572 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.572 D/DL_DLCustomView: <----
12-08 14:48:03.573 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.573 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.573 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.573 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.573 D/DL_DLCustomView: <----
12-08 14:48:03.573 I/DL_DLCustomViewGroup: [result] 1080,576
12-08 14:48:03.573 I/DL_DLCustomViewGroup: <----
12-08 14:48:03.573 D/DL_DLCustomView: onMeasure: ---->
12-08 14:48:03.573 D/DL_DLCustomView: [width] mode:EXACTLY , size:144
12-08 14:48:03.573 D/DL_DLCustomView: [height] mode:EXACTLY , size:144
12-08 14:48:03.573 D/DL_DLCustomView: [result] 144,144
12-08 14:48:03.573 D/DL_DLCustomView: <----
12-08 14:48:03.574 I/DL_DLFrameLayout: [result] 1080,1536
12-08 14:48:03.574 I/DL_DLFrameLayout: <----
12-08 14:48:03.574 I/DL_DLFrameLayout: onLayout: false [0,0,1080,1536]
12-08 14:48:03.574 I/DL_DLCustomViewGroup: onLayout: false [0,0,1080,576]
12-08 14:48:03.574 D/DL_DLCustomView: onLayout: false [0,0,144,144]
12-08 14:48:03.574 D/DL_DLCustomView: onLayout: false [144,144,288,288]
12-08 14:48:03.574 D/DL_DLCustomView: onLayout: false [288,288,432,432]
12-08 14:48:03.574 D/DL_DLCustomView: onLayout: false [432,432,576,576]
12-08 14:48:03.574 D/DL_DLCustomView: onLayout: false [0,696,144,840]
12-08 14:48:03.575 I/DL_DLFrameLayout: onDraw
12-08 14:48:03.575 I/DL_DLCustomViewGroup: onDraw
12-08 14:48:03.575 D/DL_DLCustomView: onDraw
12-08 14:48:03.575 D/DL_DLCustomView: onDraw
12-08 14:48:03.576 D/DL_DLCustomView: onDraw
12-08 14:48:03.576 D/DL_DLCustomView: onDraw
12-08 14:48:03.576 D/DL_DLCustomView: onDraw