參考:Android開發(fā)藝術(shù)探索一書
MeasureSpec##
MeasureSpec 由view自身的layoutparams(xml 中指定) 和 父容器的約束(父容器大小)來 共同生成优妙;
MeasureSpec 與測量相關(guān)的類乘综,是一個(gè)32位的int值,其中高2位表示SpecMode套硼,低30位表示SpecSize;
SpecMode 是測量模式卡辰,SpecSize指在某種測量模式下的大小邪意;
SpecMode有3種:
- UNSPECIFIED:未指定狀態(tài)九妈;
- EXACTLY:表示精確大小,LayoutParams中的 match_parent 與具體數(shù)值就是這個(gè)雾鬼;
- AL_MOST: 至多萌朱,父容器指定了一個(gè)可用大小的SpecSize,view 的大小不能大于這個(gè) SpecSize策菜,
LayoutParams 中wrap_content與之對應(yīng)晶疼;
LayoutParams##
給View設(shè)置LayoutParams,view的LayoutParams會(huì)在父容器的約束下又憨,轉(zhuǎn)換成對應(yīng)的MeasureSpec翠霍,然后再跟進(jìn)此MeasureSpec來確定View的寬高;
注意: MeasureSpec不是唯一由LayoutParams決定的蠢莺,View 的 LayoutParams與其父容器一起決定出來View的MeasureSpec寒匙;
DecorView:
DecorView是頂層父容器,她的MeasureSpec創(chuàng)建時(shí)根據(jù)屏幕大小來的;
規(guī)則:
- 當(dāng)View采用固定寬浪秘、高時(shí)蒋情,不管父容器的 MeasureSpec 是什么,View的MeasureSpec都是精確模式(EXACTLY)耸携,并且其大小遵循 LayoutParams 設(shè)置的大锌醚ⅰ;
- 當(dāng)View的寬高為match_parent時(shí)夺衍,如果父容器是精準(zhǔn)模式狈谊,則View也是精準(zhǔn)模式,并且大小是父容器的剩余空間(父容器可能有 margin)沟沙,如果父容器是最大模式河劝,則View也是最大模式,大小不超過父容器的剩余空間矛紫;
- 當(dāng)View的寬高wrap_content,不管父容器的模式是啥赎瞎,View的模式總是最大化并且大小不超過父(這里指的是自定義view,繼承view颊咬,沒有重寫onMeasure方法的情況务甥;TextView 等系統(tǒng)控件有具體的onMeasure實(shí)現(xiàn));
View measure的過程##
先從 ViewGroup類中的measureChildWithMargins方法入手:
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 的時(shí)候喳篇,會(huì)先調(diào)用 getChildMeasureSpec方法來獲得子view的MeasureSpec敞临;子View的MeasureSpec明顯與父容器有關(guān);
getChildMeasureSpec方法代碼如下:
/**
* @param spec: 父容器的 MeasureSpec
* @param padding:父容器已占用的空間大小麸澜,意思是子view布局大小時(shí)挺尿,需要減去這部分空間
@param childDimension: 子View的LayoutParams 設(shè)置的大小
***/
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);
}
如果原始View是一個(gè)view炊邦,通過measure方法就完成了其測量過程编矾,如果是ViewGroup,除了完成自己的測量過程铣耘,還要負(fù)責(zé)調(diào)用所有子元素的measure方法洽沟,子元素在遞歸執(zhí)行;
View的measure過程來measure方法來完成蜗细,measure方法會(huì)調(diào)用View的onMeasure方法:
/**
* @param widthMeasureSpec 父容器寬 MeasureSpec
**/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 設(shè)置測量后的寬高 setMeasureDimension()
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
// 返回 MeasureSpec的 specSize裆操, UNSPECIFIED 模式時(shí),大小為 getSuggestedMinimumWidth()
public static int getDefaultSize(int size, int measureSpec) {
int result = 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;
}
// 獲取最小寬度炉媒,最小寬度 為 minWidth屬性 對應(yīng) android:minWidth踪区,或者 background 背景的寬
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
View measure 調(diào)用嵌套圖,最先執(zhí)行 getSuggestedMinXXX吊骤,逐步往上走
ViewGroup measure 過程
Viewgroup 沒有重寫 View 的 onMeasure方法缎岗,但提供了 measureChildren 方法,
如果 孩子 不是 GONE,則分別用 measureChild來測量孩子白粉;
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];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
// 獲取子View的 MeasureSpec
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
// 調(diào)用view的measure方法來測量传泊,這里鼠渺,就會(huì)走 上面的 view 的 measure了
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
因?yàn)閂iewGroup是抽象類,其測量過程由子類實(shí)現(xiàn)眷细,如:LinearLayout
View的measure過程是比較復(fù)雜的拦盹,通過 measure后,就可以通過getMeasureHeight() getMeasureWidth 獲取測量后的高寬了溪椎;但最終的寬高是在 onLayout方法中取獲取普舆,因?yàn)橄到y(tǒng) 可能多次調(diào)用 measure過程;