之前不是寫了篇名為 Android 獲取 View 寬高的常用正確方式饿这,避免為零 的總結(jié)性文章嘛,在結(jié)尾簡(jiǎn)單闡述 measuredWidth 與 width 的區(qū)別撞秋〕づ酰考慮到文章的重點(diǎn),簡(jiǎn)單幾筆帶過(guò)吻贿。沒曾想串结,引發(fā)一些爭(zhēng)論,大家對(duì) View 的這兩對(duì)寬高屬性理解各有異議舅列。于是便想追根溯源肌割,通過(guò)解讀源碼的方式說(shuō)明一下,消除許多人的誤解剧蹂。
備注:由于 height 和 measuedHeight 原理也都一樣声功,為了精簡(jiǎn)語(yǔ)言,就不再重復(fù)敘說(shuō)宠叼。
width 和 measuredWidth 的誤解
先來(lái)看看大家誤解的點(diǎn)在哪里先巴。很多人包括網(wǎng)上很多資料也都是這么介紹的其爵,初學(xué) Android 時(shí)我也曾被這樣的言論誤導(dǎo)過(guò):
width 表示 View 在屏幕上可顯示的區(qū)域大小,measuredWidth 表示 View 的實(shí)際大小伸蚯,包括超出屏幕范圍外的尺寸摩渺;甚至有這樣的公式總結(jié)到:
getMeasuredWidth() = visible width + invisible width;
一個(gè)簡(jiǎn)單的例子就足以說(shuō)明這樣的解釋是錯(cuò)誤的。寫一個(gè)簡(jiǎn)單的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_sample"
android:layout_width="tv_sample"
android:layout_height="wrap_content"
android:text="This is a sample."
android:background="@android:color/darker_gray"/>
</LinearLayout>
包含一個(gè)寬高自適應(yīng)的 TextView 控件剂邮,在 Activity 中通過(guò)下面的代碼獲取 width 和 measuredWidth 屬性值:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.i("size", "The width is " + mSampleTv.getWidth());
Log.i("size", "The measured width is " + mSampleTv.getMeasuredWidth());
}
運(yùn)行結(jié)果如圖:
logcat 控制臺(tái)打印如下:
04-04 16:25:52.191 31669-31669/com.yifeng.samples I/size: The width is 314
04-04 16:25:52.191 31669-31669/com.yifeng.samples I/size: The measured width is 314
這里所用設(shè)備的屏幕尺寸為 1080x1920摇幻,足以容納這個(gè) TextView 的寬度。Log 顯示挥萌,width 和 measuredWidth 大小相同绰姻,均為 314 px。然后修改 android:layout_width 屬性值引瀑,使其超出屏幕寬度狂芋,同時(shí),將文本內(nèi)容加長(zhǎng)一些:
...
<TextView
android:id="@+id/tv_sample"
android:layout_width="2000px"
android:layout_height="wrap_content"
android:text="This is a long sample.This is a long sample.This is a long sample.This is a long sample."
android:background="@android:color/darker_gray"/>
...
再次運(yùn)行憨栽,效果如圖:
顯然帜矾,文本內(nèi)容已經(jīng)超過(guò)屏幕寬度,顯示到屏幕之外的區(qū)域屑柔。如果按照前面的言論的話屡萤,width 應(yīng)該為屏幕上顯示的寬度,即 1080 px掸宛,而 measuredWidth 肯定大于 width死陆。事實(shí)真的如此嗎,請(qǐng)看 log 日志:
04-04 16:36:47.329 6974-6974/com.yifeng.samples I/size: The width is 2000
04-04 16:36:47.330 6974-6974/com.yifeng.samples I/size: The measured width is 2000
width 等于 measuredWidth旁涤,都為 2000 px翔曲,事與愿違,與我們想象的不一樣劈愚,前面的言論也就不攻自破瞳遍。那到底 width 和 measuredWidth 有什么區(qū)別呢,我們從源碼的角度跟進(jìn)一下菌羽。
getMeasuredWidth 源碼剖析
先看 getMeasuredWidth() 方法的源碼:
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
這里有個(gè)與運(yùn)算掠械,其中,MEASURED_SIZE_MASK 是個(gè)常量值:
public static final int MEASURED_SIZE_MASK = 0x00ffffff;
換算成二進(jìn)制是:111111111111111111111111注祖,在與運(yùn)算中猾蒂,1 與任何數(shù)字進(jìn)行與運(yùn)算的結(jié)果都取決于對(duì)方。所以是晨,mMeasuredWidth 變量值決定了 getMeasuredWidth() 方法的返回值肚菠。進(jìn)一步查看,mMeasuredWidth 的賦值在這里:
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
但這個(gè)方法是私有方法罩缴,在 View 類內(nèi)部調(diào)用蚊逢,在這里:
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
boolean optical = isLayoutModeOptical(this);
// 只展示核心代碼
...
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}
可以看出层扶,mMeasuredWidth 的賦值,即 getMeasuredWidth() 的取值最終來(lái)源于 setMeasuredDimension() 方法調(diào)用時(shí)傳遞的參數(shù)烙荷!在自定義 View 時(shí)測(cè)量并設(shè)置 View 寬高時(shí)經(jīng)常用到镜会。通常在 onMeasure() 方法中設(shè)置,可以翻看一下系統(tǒng)中的 TextView终抽、LinearLayout 等方法戳表,都是如此。
getWidth 源碼剖析
再看 getWidth() 方法的源碼:
public final int getWidth() {
return mRight - mLeft;
}
mRight昼伴、mLeft 變量分別表示 View 相對(duì)父容器的左右邊緣位置匾旭,并且二者的賦值是通過(guò) setFrame() 方法中的參數(shù)獲取的:
protected boolean setFrame(int left, int top, int right, int bottom) {
...
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
...
}
setFrame() 方法中有這么一句注釋,表明該方法的調(diào)用來(lái)自 layout() 方法:
Assign a size and position to this view.
This is called from layout.
那么我們?cè)倏匆幌?layout() 方法:
public void layout(int l, int t, int r, int b) {
...
boolean changed = isLayoutModeOptical(mParent) ?
setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
...
}
其中也確實(shí)調(diào)用 setFrame() 方法亩码。所以季率,getWidth() 的取值最終來(lái)源于 layout() 方法的調(diào)用。通常描沟,layout() 方法在 parent 中被調(diào)用,來(lái)確定 child views 在父容器中的位置鞭光,一般在自定義 ViewGroup 的 onLayout() 方法中調(diào)用吏廉。
使用場(chǎng)景小結(jié)
分析完源碼,至少能夠知道:measuredWidth 值在 View 的 measure 階段決定的惰许,是通過(guò) setMeasuredDimension() 方法賦值的席覆;width 值在 layout 階段決定的,是由 layout() 方法決定的汹买。有一點(diǎn)需要注意佩伤,通常來(lái)講,View 的 width 和 height 是由 View 本身和 parent 容器共同決定的晦毙。
一般情況下生巡,getWidth() 與 getMeasuredWidth() 的返回值是相同的。在自定義 ViewGroup 時(shí)见妒,會(huì)在 onLayout() 方法中通過(guò) child.getMeasuredWidth() 方法獲取 child views 的原始大小來(lái)設(shè)置其顯示區(qū)域(諸如 LinearLayout 之類的系統(tǒng)中的 ViewGroup 都是這么做的)孤荣;除此之外,我們都可以通過(guò) getWidth() 方法獲取 View 的實(shí)際顯示寬度须揣。