android View onMeasure

134 篇文章 0 订阅
113 篇文章 0 订阅

基于4.4 code:

View.java:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
.............................................................................
    protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
        .....................................................................
        mMeasuredWidth = measuredWidth;
        mMeasuredHeight = measuredHeight;

        mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
    }
..............................................................................
/**
     * 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;
    }

之前光顾看基类ViewGroup了,结果发现 View的onMeasure中通过setMeasuredDimension()设置的View的mMeasuredWidth/Height 在 ViewGroup中根本

没有被用到,很是疑惑,感觉measure的意义没了,后来看了细化出来的FrameLayout等layout类,才发现,在这些真正的按照某种规则排布子View的类中

在onMeasure和onLayout中都是会用到子view的getMeasuredWidth/height()的,其实看到ViewGroup的onLayout是抽象函数时,应该有所察觉的。

而MeasuredWidth/Height 也基本只有在父View onMeasure/onLayout的情况下有用。

在子view的layout被调用时,会调到setFrame(l, t, r, b),这个函数才是真正的设置了View的尺寸,不一定就是之前measure时的MeasuredHeight。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值