View.onMeasure方法如何在Android中使用

本篇文章為大家展示了View.onMeasure方法如何在Android中使用 ,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司是一家成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,提供網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需定制網(wǎng)站,網(wǎng)站開發(fā)公司,公司2013年成立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營并提出專業(yè)建議和思路。

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

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

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

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

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

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

接下來的代碼片段給出了如何重寫onMeasure.注意,調(diào)用的本地空方法是來計(jì)算高度和寬度的.它們會(huì)譯解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會(huì)占據(jù)剩余容器的空間,所以它大小是確定的。

而當(dāng)設(shè)置為 wrap_content時(shí),容器傳進(jìn)去的是AT_MOST, 表示子view的大小最多是多少,這樣子view會(huì)根據(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會(huì)占據(jù)剩下容器的空間,而不會(huì)覆蓋前面已布局好的其他view空間,當(dāng)然后面布局子 view就沒有空間給分配了,所以fill_parent屬性對布局順序很重要。以前所想的是把所有容器的空間都占滿了,難怪google在2.2版本里把fill_parent的名字改為match_parent.

在兩種情況下,你必須絕對的處理這些限制。在一些情況下,它可能會(huì)返回超出這些限制的尺寸,在這種情況下,你可以讓父元素選擇如何對待超出的View,使用裁剪還是滾動(dòng)等技術(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;
}

上述內(nèi)容就是View.onMeasure方法如何在Android中使用 ,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:View.onMeasure方法如何在Android中使用
URL標(biāo)題:http://muchs.cn/article12/gecggc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、小程序開發(fā)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)