普通View的measure過程
這里的普通View是指對應ViewGroup而言的。View的measure過程是由其measure()
方法來完成的,measure()
方法是一個final類型的方法。這意味著子類不能重寫此方法。但是我們發現,View的measure()
中會去調用View的onMeasure()
方法,因此只需要看onMeasure的實現即可,View的onMeasure
的實現如下所示。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
//注解一
}
注解一:
直接調用了setMeasuredDimension(int widthSize, int heightSize)
方法,里面需要輸入兩個int形參,分別是得到的寬和高的測量尺寸。上面代碼中,widthSize對應getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec)
,heightSize對應著getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)
。那么getDefaultSize()
又是什么樣的呢?
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;
}
從上面的代碼可以看出,getDefaultSize()
這個方法邏輯很簡單:
- 如果該View的SpecMode是AT_MOST和EXACTLY兩種情況,則
getDefaultSize()
返回的數值,也就是該View測量后的對應大小(寬或者是高),就是對應的MeasureSpec中的SpecSize。 - 如果該View的SpecMode是UNSPECIFIED,這種情況一般是系統內部測量過程,此時,
getDefaultSiez()
方法會返回第一個參數size,以寬為例,即getSuggestedMinimumWidth()
方法。根據源碼,該方法會返回值的邏輯:如果該View沒有設置背景,那么直接返回android:minWidth
這個屬性所指定的值,這個值可以為0;如果View設置了背景,則返回android:minWidth
和背景的最小寬度兩者中的最大值。
上面的過程就是普通View的Measure過程,可以看出View的寬/高就是specSize決定的。同時,假如該View的布局屬性是wrap_content,那么最后設置的測量后寬/高也為specSize。這種情況就和我們預想的不一樣了。如果一個View的布局屬性是wrap_content,那么它的測量寬/高應該是大于小于specSize,但是這是卻直接設置為specSize。所以直接繼承View的自定義控件需要重寫onMeasure()
方法,設置wrap_content情況下的自身大小,否則wrap_content屬性就和match_parent屬性相同了。重寫的方式如下:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec); //先按照父類的方法計算,下面就行覆蓋
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec):
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec):
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec):
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec):
if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
setMeasuredDimension(mWidth,mHeight); //注解二
} else if (widthSpecMode == AT_MOST) {
setMeasureDimension(mWidth,heightSpecSize);
} else if (heightSpecMode == AT_MOST){
setMeasureDimension(widthSpecSize,mHeight);
}
}
注解二:
這里我們提前給View指定了一個默認的內部寬/高(mWidth,mHeight),并在wrap_content時設置此寬/高。同時,這里的mWidth和mHeight需要靈活指定,這樣才和和wrap_content屬性相符合。
ViewGroup的measure過程
ViewGroup除了需要完成自己的measure過程外,還需要遍歷所有的子元素的measure方法,各個子元素去遞歸執行這個過程。和View不同的是,ViewGroup是一個抽象類,因此它沒有重寫View的onMeasure()
方法(在繼承ViewGroup的類中,比如LinearLayout,一般都重寫了這個方法),但是提供了一個measureChildren()
的方法,如下:
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);
}
}
}
從上面的代碼可以看出,ViewGroup通過measureChildren()
方法中for循環,對它所有的Visibility屬性不等于GONE的子View執行measureChild()
方法,measureChild()
方法的實現如下:
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasure){
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);
}
上面的代碼的思路很清晰,首先通過getChildMeasureSpec()
方法獲得子View的MeasureSpec,然后調用子View的onMeasure()
方法。通過上面的方法,就可以包裝ViewGroup中所有的子View的會被測量(measure)。
但是,ViewGroup并沒有定義其測量的具體過程,即沒有重寫其父類View的measure()
方法。這是因為ViewGroup是一個抽象類,不同的ViewGroup子類有不同的布局特性,所以需要這些子類自己去實現onMeasure()
方法。不同的布局屬性,實現方式不同。但是總體思路和View的onMeasure()
方法是一樣的,根據ViewGroup的不同SpecMode(UNSPECIFIED,AT_MOST,EXACTLY)來設置不同的測量值。
補充
上面已經對View的measure過程進行了詳細的分析,現在考慮一種情況,比如我們想在Acitivity已啟動的時候獲取某個View的寬/高,這時候應該怎么做?如果直接在onCreate()
或者onResume()
方法里面去獲取這個View的寬/高,是無法正確得到某個View的寬/高信息,這是因為View的measure過程和Activity的生命周期不是同步執行的,因此無法保證Activity執行了onCreate()
,onStart()
,onResume()
時某個View已經測量完畢了,如果View沒有測量完畢,那么獲得的寬/高數據就是0。這里給出下面的四種方法:
- Activity中的
onWindowFocusChanged()
方法
這個方法意味著:View已經初始化完畢了,寬/高都已經準備好了。但是,onWindowFocusChanged()
方法會被調用多次,當Activity的窗口得到焦點或者失去焦點時均會被調用一次。具體來說,就是onResume()
和onPause()
方法執行時,onWindowFocusChanged()
方法就會被調用。典型代碼如下:
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
int width = view.getMeasureWidth();
int height = view.getMeasureHeight();
}
}
- 使用view.post(runnable)方法
通過post方法可以將一個runnable投遞到消息隊列的尾部,然后等待Looper調用此runnable的時候,View也已經初始化好了。典型代碼如下:
protected void onStart(){
super.onStart();
view.post(new Runnable(){
@override
public void run(){
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
}
});
}
- ViewTreeObserver
使用ViewTreeOberser的眾多回調可以完成這個功能。比如使用OnGlobalLayoutListener
這個接口,當View樹的狀態發生改變或View樹內部的View的可見性發生改變時,OnGlobalLayoutListener
接口中的onGlobalLayout()
方法將被回調。但是,隨著View樹的狀態改變等,onGlobalLayout()
方法將被多次嗲用。典型代碼如下:
protected void onStart(){
super.onStart();
ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@SuppressWarnings("deprecation");
@override
public void onGlobalLayout(){
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
}
});
}
- 通過view.measure(int widthMeasureSpec,int heightMeasureSpec)
通過手動對View進行measure來得到寬/高,這種方法需要根據View的LayoutParams
- match_parent 無法知道parentSize,所以這種情況無法measure出具體的寬/高
- 具體數值:直接measure
- wrap_content:MeasureSpec= (wrap_content) + 11111...1(最大值)