android 中 getWidth() 和getMeasureWidth()的区别,移动app快速开发框架

本文详细介绍了Android中视图测量的相关概念,包括`getMeasuredWidth()`与`getWidth()`的区别。`getMeasuredWidth()`返回的是视图的原始测量宽度,而`getWidth()`提供的是实际布局中的宽度,可能受到加减因素的影响。文章还探讨了测量流程,如`onMeasure()`、`setMeasuredDimension()`和`setMeasuredDimensionRaw()`的作用,以及`MEASURED_SIZE_MASK`在测量过程中的意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • {@hide}

  • 同理
    */
    protected int mRight;

  • 所以,这只是一个通过加减得到的一个相对值,并不是真正测量得到的尺寸;

getMeasuredWidth
  • Like {@link #getMeasuredWidthAndState()}, but only returns the
  • raw width component (that is the result is masked by
  • {@link #MEASURED_SIZE_MASK}).
  • @return The raw measured width of this view. 原始宽度

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

  • 这里有两个变量,一个是 mMeasuredWidth,一个是 MEASURED_SIZE_MASK

/**

  • Bits of {@link #getMeasuredWidthAndState()} and
  • {@link #getMeasuredWidthAndState()} that provide the actual(真实的) measured size.
    */
    public static final int MEASURED_SIZE_MASK = 0x00ffffff;

MEASURED_SIZE_MASK 是一个16进制的树,这里需要说明一下,view 的测量结果,它封装在:MeasureSpec类中,主要包含测量结果测量模式,有三种,分别是:MeasureSpec.UNSPECIFIED,MeasureSpec.AT_MOST,MeasureSpec.EXACTLY,MeasureSpec通过makeMeasureSpec() 方法将模式大小,封装在了一起,一个int值,高2位是测量模式,低30位是view的尺寸;

mMeasuredWidth 的值是怎么设置上的;

  • 流程子view 的 onMeasure()---->setMeasuredDimension()----->setMeasuredDimensionRaw()

  • 第一步 :setMeasuredDimension();

/**

  • This method must be called by {@link #onMeasure(int, int)} to store the

  • measured width and measured height. Failing to do so will trigger an
  • exception at measurement time.
  • 这个方法必须在onMeasure(int, int)中调用
  • @param measuredWidth The measured width of this view. May be a complex
  • bit mask as defined by {@link #MEASURED_SIZE_MASK} and
  • {@link #MEASURED_STATE_TOO_SMALL}.
  • @param measuredHeight The measured height of this view. May be a complex
  • bit mask as defined by {@link #MEASURED_SIZE_MASK} and
  • {@link #MEASURED_STATE_TOO_SMALL}.
    */
    protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
    boolean optical = isLayoutModeOptical(this)

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

;
if (optical != isLayoutModeOptical(mParent)) {
Insets insets = getOpticalInsets();
int opticalWidth = insets.left + insets.right;
int opticalHeight = insets.top + insets.bottom;

measuredWidth += optical ? opticalWidth : -opticalWidth;
measuredHeight += optical ? opticalHeight : -opticalHeight;
}
// 设置view的原始尺寸
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}

第二步:setMeasuredDimensionRaw();

/**
*

  • Sets the measured dimension without extra processing for things like optical bounds.
  • Useful for reapplying consistent values that have already been cooked with adjustments
  • for optical bounds, etc. such as those from the measurement cache.
  • @param measuredWidth The measured width of this view. May be a complex
  • bit mask as defined by {@link #MEASURED_SIZE_MASK} and
  • {@link #MEASURED_STATE_TOO_SMALL}.
  • @param measuredHeight The measured height of this view. May be a complex
  • bit mask as defined by {@link #MEASURED_SIZE_MASK} and
  • {@link #MEASURED_STATE_TOO_SMALL}.
    */
    private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
    mMeasuredWidth = measuredWidth;
    mMeasuredHeight = measuredHeight;
    mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值