從源碼的角度分析,getWidth() 與 getMeasuredWidth() 的不同之處

yibei1432 7年前發布 | 8K 次閱讀 安卓開發 Android開發 移動開發

之前不是寫了篇名為 Android 獲取 View 寬高的常用正確方式,避免為零 的總結性文章嘛,在結尾簡單闡述 measuredWidth 與 width 的區別。考慮到文章的重點,簡單幾筆帶過。沒曾想,引發一些爭論,大家對 View 的這兩對寬高屬性理解各有異議。于是便想追根溯源,通過解讀源碼的方式說明一下,消除許多人的誤解。

備注:由于 height 和 measuedHeight 原理也都一樣,為了精簡語言,就不再重復敘說。

width 和 measuredWidth 的誤解

先來看看大家誤解的點在哪里。很多人包括網上很多資料也都是這么介紹的,初學 Android 時我也曾被這樣的言論誤導過:

width 表示 View 在屏幕上可顯示的區域大小,measuredWidth 表示 View 的實際大小,包括超出屏幕范圍外的尺寸;甚至有這樣的公式總結到:

getMeasuredWidth() = visible width + invisible width;

一個簡單的例子就足以說明這樣的解釋是錯誤的。寫一個簡單的布局文件:

<?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>

包含一個寬高自適應的 TextView 控件,在 Activity 中通過下面的代碼獲取 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());
}

運行結果如圖:

logcat 控制臺打印如下:

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

這里所用設備的屏幕尺寸為 1080x1920,足以容納這個 TextView 的寬度。Log 顯示,width 和 measuredWidth 大小相同,均為 314 px。然后修改 android:layout_width 屬性值,使其超出屏幕寬度,同時,將文本內容加長一些:

...
<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"/>
...

再次運行,效果如圖:

顯然,文本內容已經超過屏幕寬度,顯示到屏幕之外的區域。如果按照前面的言論的話,width 應該為屏幕上顯示的寬度,即 1080 px,而 measuredWidth 肯定大于 width。事實真的如此嗎,請看 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 有什么區別呢,我們從源碼的角度跟進一下。

getMeasuredWidth 源碼剖析

先看 getMeasuredWidth() 方法的源碼:

public final int getMeasuredWidth() {
    return mMeasuredWidth & MEASURED_SIZE_MASK;
}

這里有個與運算,其中,MEASURED_SIZE_MASK 是個常量值:

public static final int MEASURED_SIZE_MASK = 0x00ffffff;

換算成二進制是:111111111111111111111111,在與運算中,1 與任何數字進行與運算的結果都取決于對方。所以,mMeasuredWidth 變量值決定了 getMeasuredWidth() 方法的返回值。進一步查看,mMeasuredWidth 的賦值在這里:

private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
    mMeasuredWidth = measuredWidth;
    mMeasuredHeight = measuredHeight;

    mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}

但這個方法是私有方法,在 View 類內部調用,在這里:

protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
    boolean optical = isLayoutModeOptical(this);
    // 只展示核心代碼
    ...
    setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}

可以看出,mMeasuredWidth 的賦值,即 getMeasuredWidth() 的取值最終來源于 setMeasuredDimension() 方法調用時傳遞的參數!在自定義 View 時測量并設置 View 寬高時經常用到。通常在 onMeasure() 方法中設置,可以翻看一下系統中的 TextView、LinearLayout 等方法,都是如此。

getWidth 源碼剖析

再看 getWidth() 方法的源碼:

public final int getWidth() {
    return mRight - mLeft;
}

mRight、mLeft 變量分別表示 View 相對父容器的左右邊緣位置,并且二者的賦值是通過 setFrame() 方法中的參數獲取的:

protected boolean setFrame(int left, int top, int right, int bottom) {
        ...
        mLeft = left;
        mTop = top;
        mRight = right;
        mBottom = bottom;
        ...
}

setFrame() 方法中有這么一句注釋,表明該方法的調用來自 layout() 方法:

Assign a size and position to this view.

This is called from layout.

那么我們再看一下 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);
   ...
}

其中也確實調用 setFrame() 方法。所以,getWidth() 的取值最終來源于 layout() 方法的調用。通常,layout() 方法在 parent 中被調用,來確定 child views 在父容器中的位置,一般在自定義 ViewGroup 的 onLayout() 方法中調用。

使用場景小結

分析完源碼,至少能夠知道:measuredWidth 值在 View 的 measure 階段決定的,是通過 setMeasuredDimension() 方法賦值的;width 值在 layout 階段決定的,是由 layout() 方法決定的。有一點需要注意,通常來講,View 的 width 和 height 是由 View 本身和 parent 容器共同決定的。

一般情況下,getWidth() 與 getMeasuredWidth() 的返回值是相同的。在自定義 ViewGroup 時,會在 onLayout() 方法中通過 child.getMeasuredWidth() 方法獲取 child views 的原始大小來設置其顯示區域(諸如 LinearLayout 之類的系統中的 ViewGroup 都是這么做的);除此之外,我們都可以通過 getWidth() 方法獲取 View 的實際顯示寬度。

 

來自:http://yifeng.studio/2017/03/30/the-difference-between-getWidth-and-getMeasuredWidth/

 

 本文由用戶 yibei1432 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!