美文网首页
Android 源码分析问题(二)—— onMeasure()

Android 源码分析问题(二)—— onMeasure()

作者: JzyCc | 来源:发表于2019-02-10 23:35 被阅读0次

问题引入

某些时候 onMeasure() 会被调用两次,这个问题让我比较困扰,这里我找到了相关源码来解释这个问题。

本文只针对该问题,暂不对其它涉及到的内容分析。

测量的开始

对于 Activity 中 view 的测量,布局,绘制三个过程大部分人都已经比较熟悉了,这里我们直接从一个 Activity 如何开始这三个过程开始找到问题。

第一次测量在什么时候?

那我们需要先知道 ViewRootImpl, 这个类可以理解成它的名字,它是一个 Activity 中所有 View 的 root。

而我们所熟悉的 View 的三板斧以及事件分发就是以它为根节点,向下传递的。但是它本身并不是一个 View,或者说它是实现了一个转接,分发的作用,它的作用性可想而至。

这里直接看 ViewRootImpl 开始测量根部 View (DecorView) 的方法。

performTraversals()

这里节选其中的一部分代码。

performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

// Implementation of weights from WindowManager.LayoutParams
// We just grow the dimensions as needed and re-measure if
// needs be
int width = host.getMeasuredWidth();
int height = host.getMeasuredHeight();
boolean measureAgain = false;

if (lp.horizontalWeight > 0.0f) {
        width += (int) ((mWidth - width) * lp.horizontalWeight);
        childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
        MeasureSpec.EXACTLY);
        measureAgain = true;
}
if (lp.verticalWeight > 0.0f) {
       height += (int) ((mHeight - height) * lp.verticalWeight);
       childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
       MeasureSpec.EXACTLY);
       measureAgain = true;
}

if (measureAgain) {
       if (DEBUG_LAYOUT) Log.v(mTag,
               "And hey let's measure once more: width=" + width
               + " height=" + height);
      performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
 }

可以看到其中有一个 measureAgain 的标示,以及设置为 true 的两个条件,这里不做额外的篇幅详解,只是简单介绍 verticalWeight 和 horizontalWeight 两个字段的含义。

在第一次 performMeasure() 方法调用后, 如果子View 需要的空间大于父容器为它测量的大小,那么对应的 verticalWeight 和 horizontalWeight 将会大于0,即这两个字段分别对应垂直和水平的情况下子 View 需要的额外空间。

这时候会将 measureAgain 设置为 true, 并且开始第二次测量。

总结

对于不满意父容器测量的子 View, 父容器将会依据 子 View 的测量结果来开始第二次测量。

结语

这里是我个人的关于这个问题的大致分析,可能不够全面或者有什么错误的地方,还望指出。

相关文章

网友评论

      本文标题:Android 源码分析问题(二)—— onMeasure()

      本文链接:https://www.haomeiwen.com/subject/nhlfeqtx.html