最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android View.onMeasure方法詳解及實(shí)例

 更新時(shí)間:2017年05月06日 16:44:23   投稿:lqh  
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

Android View.onMeasure方法詳解及實(shí)例

View在屏幕上顯示出來要先經(jīng)過measure(計(jì)算)和layout(布局).

1、什么時(shí)候調(diào)用onMeasure方法?

當(dāng)控件的父元素正要放置該控件時(shí)調(diào)用.父元素會問子控件一個(gè)問題,“你想要用多大地方???”,然后傳入兩個(gè)參數(shù)——widthMeasureSpec和heightMeasureSpec.

這兩個(gè)參數(shù)指明控件可獲得的空間以及關(guān)于這個(gè)空間描述的元數(shù)據(jù).

更好的方法是你傳遞View的高度和寬度到setMeasuredDimension方法里,這樣可以直接告訴父控件,需要多大地方放置子控件.

  接下來的代碼片段給出了如何重寫onMeasure.注意,調(diào)用的本地空方法是來計(jì)算高度和寬度的.它們會譯解widthHeightSpec和heightMeasureSpec值,并計(jì)算出合適的高度和寬度值.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}

private int measureHeight(int measureSpec) {


// Return measured widget height.
}

private int measureWidth(int measureSpec) {

// Return measured widget width.
}

邊界參數(shù)——widthMeasureSpec和heightMeasureSpec ,效率的原因以整數(shù)的方式傳入。在它們使用之前,首先要做的是使用MeasureSpec類的靜態(tài)方法getMode和getSize來譯解,如下面的片段所示:

int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

依據(jù)specMode的值,(MeasureSpec有3種模式分別是UNSPECIFIED, EXACTLY和AT_MOST)

  • 如果是AT_MOST,specSize 代表的是最大可獲得的空間;
  • 如果是EXACTLY,specSize 代表的是精確的尺寸;
  • 如果是UNSPECIFIED,對于控件尺寸來說,沒有任何參考意義。

2、那么這些模式和我們平時(shí)設(shè)置的layout參數(shù)fill_parent, wrap_content有什么關(guān)系呢?

經(jīng)過代碼測試就知道,當(dāng)我們設(shè)置width或height為fill_parent時(shí),容器在布局時(shí)調(diào)用子 view的measure方法傳入的模式是EXACTLY,因?yàn)樽觱iew會占據(jù)剩余容器的空間,所以它大小是確定的。

而當(dāng)設(shè)置為 wrap_content時(shí),容器傳進(jìn)去的是AT_MOST, 表示子view的大小最多是多少,這樣子view會根據(jù)這個(gè)上限來設(shè)置自己的尺寸。當(dāng)子view的大小設(shè)置為精確值時(shí),容器傳入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式目前還沒有發(fā)現(xiàn)在什么情況下使用。 

   View的onMeasure方法默認(rèn)行為是當(dāng)模式為UNSPECIFIED時(shí),設(shè)置尺寸為mMinWidth(通常為0)或者背景drawable的最小尺寸,當(dāng)模式為EXACTLY或者AT_MOST時(shí),尺寸設(shè)置為傳入的MeasureSpec的大小。 

   有個(gè)觀念需要糾正的是,fill_parent應(yīng)該是子view會占據(jù)剩下容器的空間,而不會覆蓋前面已布局好的其他view空間,當(dāng)然后面布局子 view就沒有空間給分配了,所以fill_parent屬性對布局順序很重要。以前所想的是把所有容器的空間都占滿了,難怪google在2.2版本里把fill_parent的名字改為match_parent.

  在兩種情況下,你必須絕對的處理這些限制。在一些情況下,它可能會返回超出這些限制的尺寸,在這種情況下,你可以讓父元素選擇如何對待超出的View,使用裁剪還是滾動等技術(shù)。

  接下來的框架代碼給出了處理View測量的典型實(shí)現(xiàn):

java代碼:

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int measuredHeight = measureHeight(heightMeasureSpec);

int measuredWidth = measureWidth(widthMeasureSpec);

setMeasuredDimension(measuredHeight, measuredWidth);

}

private int measureHeight(int measureSpec) {

int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

// Default size if no limits are specified.

int result = 500;
if (specMode == MeasureSpec.AT_MOST){

// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.

result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY){

// If your control can fit within these bounds return that value.
result = specSize;
}

return result;
}

private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST){
// Calculate the ideal size of your control
// within this maximum size.
// If your control fills the available space
// return the outer bound.
result = specSize;
}

else if (specMode == MeasureSpec.EXACTLY){
// If your control can fit within these bounds return that value.

result = specSize;
}

return result;
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論

古丈县| 武夷山市| 墨脱县| 吴旗县| 银川市| 大悟县| 敦化市| 深州市| 沅陵县| 牙克石市| 永州市| 阿拉善右旗| 东乌珠穆沁旗| 宽甸| 安新县| 宁陵县| 昭通市| 康平县| 乌鲁木齐县| 娱乐| 河北区| 井陉县| 太仓市| 囊谦县| 雷波县| 西吉县| 德钦县| 东丰县| 靖边县| 六枝特区| 阳原县| 皋兰县| 舞钢市| 和硕县| 柞水县| 江山市| 肥乡县| 贵溪市| 墨脱县| 南宫市| 上栗县|