安卓开发之getMeasuredWidth和getWidth的区别

本文详细探讨了Android开发中View的getMeasuredWidth()和getWidth()两个函数的区别。getMeasuredWidth()的值在setMeasuredDimension()后确定,受MEASURED_SIZE_MASK限制,而getWidth()的值在layout()后由mRight和mLeft决定,通常在ViewGroup的onLayout阶段设置。两者返回值不同时,表明layout方法传入参数与setMeasuredDimension不一致。
摘要由CSDN通过智能技术生成

一、函数getMeasuredWidth() :

可以先看一下View / ViewGroup关于 getMeasuredWidth 这个函数的源码:

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

其中 mMeasuredWidth 是由我们在View的onMeasure阶段中使用 setMeasuredDimension 这个函数传入的。

    setMeasuredDimension(int measuredWidth, int measuredHeight)

而 MEASURED_SIZE_MASK 是一个静态常量,我想它是用来限制mMeasuredWidth的大小吧。

    public static final int MEASURED_SIZE_MASK = 0x00ffffff;

所以,我们的 getMeasuredWidth() 所返回的值与 该View / ViewGroup 在调用 setMeasuredDimension 后才会有一定的值,且由 setMeasuredDimension 决定返回值的大小。

二、函数getWidth():

接下来再看一下 View 关于 getWidth 这个函数的源码:

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

而 mRight 和 mLeft 是在 View 的 setFrame / setOpticalFrame 函数中被赋值的。<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值