View的工作原理(二)

測量會用到

一。MeasureSpec,它是一個32位的int型整數。

它由兩部分組成,分別是前三位的SpecMode(MeasureSpec & 0xc000 0000) 和後29位的SpecSize(MeasureSpec &0x3FFF FFFF)組成。之所以把他們合併成MeasureSpec是爲了節省存儲空間。

   SpecMode:,表示測量的模式 有,有三種:

public static class MeasureSpec {
    public static final int AT_MOST = -2147483648;
    public static final int EXACTLY = 1073741824;
    public static final int UNSPECIFIED = 0;

         1.EXACTLY (精確模式,SpecSize的值即是View的最終大小,對應LayoutParams的match_parent 和具體數值兩種模式)

         2.AT_MOST(最大模式,View的最終大小不能超過SpecSize, 對應LayoutParams的wrap_content)

         3.UNSPECIFIED(不對View的大小做限制)    

   SpecSize:表示測量模式下的尺寸大小


MeasureSpec類中提供以下方法分別獲得SpecMode和SpecSize

public static int getMode(int measureSpec){
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec){
return (measureSpec & ~ MODE_MASK);
}
MeasureSpec類中提供以下方法獲得MeasureSpec

public static final int makeMeasureSpec(int size,int mode){
if(sUseBrokenMakeMeasureSpec){
return size + mode;}else{
 return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}

二。DecorView 的Measure

childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth,lp.width);//desireWindowWidth表示窗口
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight,lp.height);
//desireWindowHeight表示窗口高度
performMeasure(childWidthMeasureSpec,childHeightMeasureSpec);
由以上可知的:
窗口的尺寸和自身的LayoutParams傳入getRootMeasureSpec方法後可得到DecorView的MeasureSpec

private static int getRootMeasureSpec(int windowSize,int rootDimension){
int measureSpec;
case ViewGroup.LayoutParams.MATCH_PARENT:
measureSpec = MeasureSpec.makeMeasureSpec(windowSize,MeasureSpec.EXACTLY);
break;
case ViewGroup.LayoutParams.WRAP_CONTENT:
measureSpec = MeasureSpec.makeMeasureSpec(windowSize,MeasureSpec.AT_MOST);
break;
default:
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension,MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}

三。普通View的Measure

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+widhUsed,lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,mPaddingTop+mPaddingBottom+lp.topMargin+lp.bottomMargin+heightUsed,lp.height);
}
child.measue(childWidthMeasureSpec ,childHeightMeasureSpec );

由上可知:
view的MeasureSpec的獲得和DecorView有些類似,具體指由父容器的MeasureSpec和子元素的LayoutParams決定
getChildMeasureSpec的實現過程這裏就不寫了,有興趣可以參看源碼,由該函數可得:


1.如果子元素是具體值,如lp.width=100dp,則其SpecMode都是EXACTLY,SpecSize爲100


2.如果是LayoutParams.MATCH_PARENT = lp.width, 則SpecMode和父容器的MeasureSpec的SpecMode一致,SpecSize是父容器的剩餘大小


3.如果LayoutParams.WRAP_CONTENT = lp.width,則SpecMode都是AT_MOST,SpecSize是父容器的剩餘大小
                


參考:Android開放藝術探索



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章