国产探花免费观看_亚洲丰满少妇自慰呻吟_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開放藝術探索


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博罗县| 鸡东县| 平山县| 合水县| 抚松县| 尖扎县| 娄烦县| 泊头市| 丹巴县| 尉氏县| 乳山市| 南通市| 百色市| 旬阳县| 民县| 尉氏县| 余干县| 开原市| 莱芜市| 专栏| 元阳县| 鹤峰县| 青浦区| 板桥市| 苍溪县| 泗阳县| 镇安县| 剑河县| 赣榆县| 汝州市| 益阳市| 汉中市| 怀柔区| 西和县| 洛浦县| 广东省| 阿克苏市| 西乡县| 克拉玛依市| 大关县| 肇庆市|