理解MeasureSpec
Posted 呼啸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了理解MeasureSpec相关的知识,希望对你有一定的参考价值。
MesaureSpec是View的内部类。它封装了一个View的规格尺寸。包括View的宽和高的信息。它的作用就是,在Measure的过程中,系统会将View的LayoutParams根据父容器所施加的规则转换成对应的MeasureSpec,然后在onMeasure方法中根据这个MeasureSpec来确定View的宽和高。
我们来看下MeasureSpec类的代码:
public static class MeasureSpec
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
/** @hide */
@IntDef(UNSPECIFIED, EXACTLY, AT_MOST)
@Retention(RetentionPolicy.SOURCE)
public @interface MeasureSpecMode
/**
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
/**
* Measure specification mode: 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.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;
/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;
/**
* Creates a measure specification based on the supplied size and mode.
*
* The mode must always be one of the following:
* <ul>
* <li>@link android.view.View.MeasureSpec#UNSPECIFIED</li>
* <li>@link android.view.View.MeasureSpec#EXACTLY</li>
* <li>@link android.view.View.MeasureSpec#AT_MOST</li>
* </ul>
*
* <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
* implementation was such that the order of arguments did not matter
* and overflow in either value could impact the resulting MeasureSpec.
* @link android.widget.RelativeLayout was affected by this bug.
* Apps targeting API levels greater than 17 will get the fixed, more strict
* behavior.</p>
*
* @param size the size of the measure specification
* @param mode the mode of the measure specification
* @return the measure specification based on size and mode
*/
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);
/**
* Like @link #makeMeasureSpec(int, int), but any spec with a mode of UNSPECIFIED
* will automatically get a size of 0. Older apps expect this.
*
* @hide internal use only for compatibility with system widgets and older apps
*/
@UnsupportedAppUsage
public static int makeSafeMeasureSpec(int size, int mode)
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED)
return 0;
return makeMeasureSpec(size, mode);
/**
* Extracts the mode from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the mode from
* @return @link android.view.View.MeasureSpec#UNSPECIFIED,
* @link android.view.View.MeasureSpec#AT_MOST or
* @link android.view.View.MeasureSpec#EXACTLY
*/
@MeasureSpecMode
public static int getMode(int measureSpec)
//noinspection ResourceType
return (measureSpec & MODE_MASK);
/**
* Extracts the size from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the size from
* @return the size in pixels defined in the supplied measure specification
*/
public static int getSize(int measureSpec)
return (measureSpec & ~MODE_MASK);
static int adjust(int measureSpec, int delta)
final int mode = getMode(measureSpec);
int size = getSize(measureSpec);
if (mode == UNSPECIFIED)
// No need to adjust size for UNSPECIFIED mode.
return makeMeasureSpec(size, UNSPECIFIED);
size += delta;
if (size < 0)
Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
") spec: " + toString(measureSpec) + " delta: " + delta);
size = 0;
return makeMeasureSpec(size, mode);
/**
* Returns a String representation of the specified measure
* specification.
*
* @param measureSpec the measure specification to convert to a String
* @return a String with the following format: "MeasureSpec: MODE SIZE"
*/
public static String toString(int measureSpec)
int mode = getMode(measureSpec);
int size = getSize(measureSpec);
StringBuilder sb = new StringBuilder("MeasureSpec: ");
if (mode == UNSPECIFIED)
sb.append("UNSPECIFIED ");
else if (mode == EXACTLY)
sb.append("EXACTLY ");
else if (mode == AT_MOST)
sb.append("AT_MOST ");
else
sb.append(mode).append(" ");
sb.append(size);
return sb.toString();
从MeasureSpec的常量可以看出,它代表了32位的int值,其中高2位代表了specMode,低30位则代表了specSize。specMode指的是测量模式,specSize指的是测量大小。
specMode有3种模式,如下:
UNSPECIFIED:未指定模式,View想多大就多大。父容器不做限制。一般用于系统内部的测量。
AT_MOST:最大模式,对应于wrap_content属性,子View的最终大小是父View指定的specSize值,并且子View的大小不能大于这个值。
EXACTLY:精确模式。对应于match_parent属性和具体的数值,父容器测量出View所需要的大小,也就是specSize的值。
对于每一个View,都持有一个MeasureSpec,而该MeasureSpec则保存了该View的尺寸规格。在View的测量流程中,通过makeMeasureSpec来保存宽和高的信息。通过getMode或getSize得到模式和宽、高。MesaureSpec是受自身LayoutParams和父容器的MeasureSpect共同影响。那么作为顶层View的DecorView来说,它并没有父容器,那么它的MeasureSpec是如何得来的呢?我们再回头看ViewRootImpl的performTraversals方法:
private void performTraversals()
...
if(!mStopped)
int childWidthMeasureSpec = getRootMeasureSpec(mWidth,lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight,lp.height);
performLayout(lp,desiredWindowWidth,desiredWindowHeight);
if(didLayout)
performLayout(lp,desiredWindowWidth,desiredWindowHeight);
..
if(!cancelDraw && !newSureface)
if(!skipDraw || mReportNextDraw)
if(mPendingTransition != null && mPendingTransitions.size > 0)
for(int i=0; i< mPendingTransitions.size;++i)
mPendingTransitions.get(i).startChangingAimations();
mPendingTransitions.clear();
performDraw();
...
看里面这句:
int childWidthMeasureSpec = getRootMeasureSpec(mWidth,lp.width);
这里调用了getRootMearueSpec方法。下面来勘察这个方法做了什么:
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;
第一个参数windowSize指的是窗口的尺寸。所以对于DecorView来说,它的MeasureSpec由自身的LayoutParams和窗口的尺寸决定。这一点和普通的View不同。还记得普通View是怎么决定的吗?普通View是由自身的LayoutParams和父容器的MeasureSpec共同决定。我们接着看下面的代码就会看到DecorView根据自身的layoutParams来得到不同的MeasureSpec。所以,我们前面说的performMeasure方法中需要传入两个参数,即childWidthMeasureSpec和childHeightMeasureSpec所代表的意思就是chid的MesaureSpec的宽和高。接着回到performTraversals方法,看看perfromMeasure方法内部做了什么:
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec)
if (mView == null)
return;
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
try
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
finally
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
这里可以很明显的看到,调用了View的measure方法。
以上是关于理解MeasureSpec的主要内容,如果未能解决你的问题,请参考以下文章