从源码的角度分析,getWidth() 与 getMeasuredWidth() 的不同之处

之前不是写了篇名为 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());
}复制代码

运行结果如图:

《从源码的角度分析,getWidth() 与 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复制代码

这里所用设备的屏幕尺寸为 1080×1920,足以容纳这个 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"/>
...复制代码

再次运行,效果如图:

《从源码的角度分析,getWidth() 与 getMeasuredWidth() 的不同之处》

显然,文本内容已经超过屏幕宽度,显示到屏幕之外的区域。如果按照前面的言论的话,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 的实际显示宽度。

关于我:亦枫,博客地址:yifeng.studio/,新浪微博:IT亦枫

微信扫描二维码,欢迎关注我的个人公众号:安卓笔记侠

不仅分享我的原创技术文章,还有程序员的职场遐想

《从源码的角度分析,getWidth() 与 getMeasuredWidth() 的不同之处》

    原文作者:Android源码分析
    原文地址: https://juejin.im/post/58e6dc292f301e0062332883
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞