在Android中,一個View繪製出來要經過三大流程,第一是測量View的大小,第二是確定View的位置,第三是將View渲染到屏幕,本文將講解View的測量行爲,即如何確定View的大小
在View的測量過程中,首先介紹一個類MeasureSpec,這個類是用來保存測量結果的,即SpecMode和大小SpecSize,分別是View的模式和大小,MeasureSpec通過一個int值的高2位和低30分別去保存它們。其中SpecMode有三種:UNSPECIFUED、EXACTLY、AT_MOST,如下
1. EXACTLY 表示當前view爲精確模式,它對應於LayoutParams中的match_parent和具體數值
2. AT_MOST 表示當前view的大小不超過父容器的可用大小,它對應於LayoutParams中的wrap_content
3. UNSPECIFIED 表示父view不約束子view的大小,例如ListView、ScrollView等
因爲一個view的SpecMode是由父容器的SpecMode和view自己的LayoutParams來確定的,而父容器一般都是ViewGroup,所以我們先來分析Viewgroup的測量行爲
ViewGroup是一個抽象類,它本身是不能最終確認View的大小的,因爲ViewGroup可以說一個view容器,裏面的子view具體要怎麼放置由ViewGroup的繼承者來決定,不同的ViewGroup有不同的實現,比如LinearLayout或RelativeLayout,它們的對子view的排列方式都不一樣,所以說ViewGroup是無法測量出自身大小的,但它提供了一些重要的方法來測量子view的MeasureSpec,看一下ViewGroup的measureChildren方法,如下
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
//循環遍歷測量每一個child
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
可以看出,ViewGroup會對每一個子view進行measure,再看measureChild方法,代碼如下
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
//子view的layout參數
final LayoutParams lp = child.getLayoutParams();
//獲取child的MesureSpec
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
//調用chlid的measure方法進行子view的測量
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
上面代碼的關鍵是通過調用getChildMeasureSpec方法來獲取子view的測量規格MeasureSpec,然後通過child的measure方法將測量行爲傳遞給子view,來看看getChildMeasureSpec方法的代碼
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
//父view的測量模式和測量大小
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
//父view可用的最大空間
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: //父容器爲精確模式,一般爲match_parent或精確值
//子view是精確值
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
}
// 子view模式MATCH_PARENT(-1),大小爲父容器可用的最大空間
else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
}
// 子view模式WRAP_CONTENT(-2),大小爲父容器可用的最大空間
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
//父容器AT_MOST模式,一般爲wrap_content
case MeasureSpec.AT_MOST:
if (childDimension >= 0) { //子view精確值
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
}
//子view爲match_parent,大小等於父容器的最大可用空間
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;
}
//子view爲wrap_content,子view自己決定自己的大小,但不能超過父view的最大可用空間
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;
// 當父view的模式爲UNSPECIFIED時,父容器不對view有任何限制,要多大給多大,多見於ListView、GridView
// 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的MeasureSpec是怎麼來的,就是通過自身的layoutparams值和父view的MeasureSpec去計算得到MeasureSpec的,總結如下表
這裏要注意的是,默認情況下,除非子view指定了一個確切的大小,否則其測量模式不管是match_parent還是wrap_content,得到的測量大小都是父容器可用空間的大小,這點在自定義view的時候需要特別注意的
到這裏我們已經獲得了view的MeasureSpec,其中包含了view的模式和大小,但對於ViewGroup而言,這只是獲得了其子view的MeasureSpec,要想計算ViewGroup的大小需要重寫View的onMeasure方法,最終通過setMeasuredDimension來設置View的大小
下面是View的onMeasure方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//確定view的大小
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
默認情況下是通過getDefaultSize方法獲取view的寬高的大小
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;
}
主要關注AT_MOST和EXACTLY這兩種情況,其實就是返回之前我們在getChildMeasureSpec方法中計算的測量大小;在默認情況下,view的測量模式不管是match_parent還是wrap_content,其測量大小都等於父view的最大的可用大小,所以一般在自定義view的時候,對於wrap_content的情形,通常都會指定一個確切大小,以避免和match_parent的效果一樣
總結一下,如果把View分爲View和ViewGroup,View的大小依賴於其父View的MeasureSpec和自身的LayoutParams,而ViewGroup的大小則依賴於子view的大小和它們的排列方式,但不管是單一View還是ViewGroup最終的大小都在onMeasure方法中設置並確定,所以自定義View的時候我們往往重寫onMeasure方法來設置View的最終大小