国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

View的工作原理(二)

2019-11-09 18:42:55
字體:
來源:轉載
供稿:網友

測量會用到

一。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為1002.如果是LayoutParams.MATCH_PARENT = lp.width, 則SpecMode和父容器的MeasureSpec的SpecMode一致,SpecSize是父容器的剩余大小3.如果LayoutParams.WRAP_CONTENT = lp.width,則SpecMode都是AT_MOST,SpecSize是父容器的剩余大小                參考:Android開放藝術探索


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁山县| 柘荣县| 嘉祥县| 曲周县| 肥城市| 晋江市| 虞城县| 双流县| 陆良县| 远安县| 普兰店市| 墨江| 全南县| 皋兰县| 古交市| 陆良县| 高要市| 中超| 庄河市| 阿鲁科尔沁旗| 楚雄市| 美姑县| 竹北市| 普格县| 商丘市| 延庆县| 米脂县| 安乡县| 九台市| 平湖市| 靖远县| 莱芜市| 潮安县| 平阴县| 弋阳县| 虹口区| 鄂托克旗| 屯留县| 鸡西市| 化州市| 乌什县|