一蒋畜、概念
1锦茁、MeasureSpec代表一個32位int值,高2位代表SpecMode胚吁,低30位代表SpecSize撑毛。
SpecMode是測量模式书聚,SpecSize是某種測量模式下的規(guī)格大小。
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);
}
}
}
2藻雌、SpecMode的類型概念
(1)UNSPECIFIED :父容器不對View有任何限制雌续,要多大給多大,這種情況一般用于系統(tǒng)內(nèi)部蹦疑,表示一種的狀態(tài)西雀。
(2)EXACTLY:父容器已經(jīng)檢測出View所需要的精確大小,這個時候View的最終大小就是SpecSize所指定的值歉摧。對應(yīng)LayoutParams的match_parent和具體的數(shù)值兩種模式艇肴。
(3)AT_MOST:父容器指定了一個可用大小即SpecSize,View的大小不能大于這個值叁温,具體是什么值要看不同View的具體實現(xiàn)再悼,對應(yīng)于LayoutParams的wrap_content。
二膝但、與LayoutParams的對應(yīng)關(guān)系
對應(yīng)頂級View(DecorView)冲九,其MeasureSpec由窗口的尺寸和自身的LayoutParams共同確定;對于普通View,其MeasureSpec由父容器的MeasureSpec和資深的LayoutParams來共同確定莺奸,MeasureSpec一旦確定后丑孩,onMeasure就可以確定View的測量寬高。
1灭贷、DecorView的MeasureSpec創(chuàng)建過程
/**
* Figures out the measure spec for the root view in a window based on it's
* layout params.
*
* @param windowSize
* The available width or height of the window
*
* @param rootDimension
* The layout params for one dimension (width or height) of the
* window.
*
* @return The measure spec to use to measure the root view.
*/
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;
}
~LayoutParams.MATCH_PARENT:精確模式温学,大小就是窗口的大小
~LayoutParams.WRAP_CONTENT:最大模式,大小不定甚疟,但是不能超過窗口大小
~固定大姓提:精確模式,大小為LayoutParams指定大小
2览妖、一般View的MeasureSpec創(chuàng)建過程
對于普通的View轧拄,其measure過程由ViewGroup傳遞過來。
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進行measure前讽膏,ViewGroup通過getChildMeasureSpec得到子元素的MeasureSpec檩电,子View的MeasureSpec與父容器的MeasureSpec和子View本身的LayoutParams有關(guān)。
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);
}
該方法根據(jù)父布局的MeasureSpec同時結(jié)合View本身的LayoutParams來確定子元素的MeasureSpec桅打。整理成表則為:
(1)當(dāng)View采用固定寬高的時候是嗜,不管父容器的MeasureSpec是什么,View的MeasureSpec都是精確模式并且遵循LayoutParams的大小挺尾。
(2)當(dāng)View的寬高是match_parent的時候鹅搪,如果父容器是精確模式,那么View也是精確模式并且其大小時父容器的剩余空間遭铺;如果父容器是最大模式丽柿,那么View也是最大模式并且大小不會超過父容器的剩余空間。
(3)當(dāng)View的寬高是wrap_content的時候魂挂,不管父容器的模式是精確還是最大化甫题,View的模式總是最大化并且不能超過父容器的剩余空間。
結(jié)尾
摘抄記錄自《Android開發(fā)藝術(shù)探討》