从源码角度理解RelativeLayout#onMeasure对child的measure调用次数

简介: 从源码角度理解RelativeLayout#onMeasure对child的measure调用次数

熟悉绘制流程的都知道,ViewGroup可以决定child的绘制时机以及调用次数。

今天我们就从RelativeLayout开始学起,看一下它对子ViewonMeasure调用次数具体是多少。

简单起见,我们选择进入Activity的时机,在前面的blog进入Activity时,为何页面布局内View#onMeasure会被调用两次?提到过,进入页面时最少会走两遍绘制流程,我们需要观测下每次绘制流程中,child的onMeasure执行次数。

系列文章:

从源码角度理解FrameLayout#onMeasure对child的measure调用次数

从源码角度理解LinearLayout#onMeasure对child的measure调用次数

从源码角度理解RelativeLayout#onMeasure对child的measure调用次数

从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数

ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?

通过log观测现象

时机:进入页面;

xml布局:里面的自定义View都只是添加了log。

demo:RelativeLayoutTestActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".measure.RelativeLayoutTestActivity">

    <com.tinytongtong.androidstudy.measure.view.CustomRelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.tinytongtong.androidstudy.measure.view.CustomSingleView
            android:id="@+id/view1"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark" />

        <com.tinytongtong.androidstudy.measure.view.CustomTextView
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#99dddddd"
            android:text="match_parent" />

        <com.tinytongtong.androidstudy.measure.view.CustomButton
            android:id="@+id/view3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/view5"
            android:text="wrap_content" />

        <com.tinytongtong.androidstudy.measure.view.CustomImageView
            android:id="@+id/view4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@id/view5"
            android:background="@drawable/ic_launcher"
            android:contentDescription="wrap_content" />

        <com.tinytongtong.androidstudy.measure.view.CustomHelloView
            android:id="@+id/view5"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:background="@color/background_debug" />

        <com.tinytongtong.androidstudy.measure.view.CustomWorldView
            android:id="@+id/view6"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/view5"
            android:layout_centerHorizontal="true"
            android:background="@color/background_info"
            android:gravity="center" />

    </com.tinytongtong.androidstudy.measure.view.CustomRelativeLayout>

</LinearLayout>

这里给RelativeLayout添加了4个child,分别设置了不同的宽高。接着以RelativeLayout的宽高为变量,分别设置match_parentwrap_content,我们观察下对应的onMeasure执行次数。

现实效果

宽高两两组合,一共有四种情况,具体效果如下表:

自身 view1(固定宽高,layout_alignParentRight,layout_alignParentBottom) view2(w:match_parent,h:match_parent) view3(w:match_parent,h:wrap_content,android:layout_alignBaseline="@id/view5") view4(w:wrap_content,h:match_parent,android:layout_toLeftOf="@id/view5") view5(固定宽高,android:layout_centerInParent="true") view6(固定宽高,android:layout_below="@id/view5",android:layout_centerHorizontal="true") 备注:第一次绘制处理mSortedHorizontalChildren,child的顺序是6、5、4、3、2、1;第二次绘制处理mSortedVerticalChildren,顺序是5、3、6、4、2、1;
match_parent match_parent M:2,L:1,D:0(默认不参与onDraw) M:4,L:1,D:1(参与第一、二次onMeasure) M:4,L:1,D:1(参与第一、二次onMeasure) M:4,L:1,D:2(参与第一、二次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure) M:4,L:1,D:1(参与第一、二次onMeasure) M:4,L:1,D:1(参与第一、二次onMeasure)
match_parent wrap_content M:2,L:1,D:0 同上 同上 同上 同上 同上 同上
wrap_content match_parent M:2,L:1,D:0 同上 同上 同上 同上 同上 同上
wrap_content wrap_content M:2,L:1,D:0 同上 同上 同上 同上 同上 同上

说明:
MonMeasureLonLayoutDonDraw
M:2,L:1,D:1 表示onMeasure调用了2次,onLayout调用了1次,onDraw调用了一次。

我们知道,进入Activity时,最少会走两次onMeasure方法,具体请看进入Activity时,为何页面布局内View#onMeasure会被调用两次?

观察表格中的内容,我们发现在一次测量流程中,RelativeLayout的child,最少是一次测量最多是两次测量依赖关系会对测量次数有影响。

RelativeLayout#onMeasure(int widthMeasureSpec, int heightMeasureSpec)源码分析

具体是怎样的情况呢?我们看下源码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // 准备数据。遍历所有child,将不包含水平依赖的child添加进mSortedHorizontalChildren;
    // 将不包含垂直依赖的child添加进mSortedVerticalChildren。
    if (mDirtyHierarchy) {
        mDirtyHierarchy = false;
        sortChildren();
    }

    ...
    // 第一次测量,测量所有不包含水平依赖关系的child
    View[] views = mSortedHorizontalChildren;
    int count = views.length;
    for (int i = 0; i < count; i++) {
        View child = views[i];
        if (child.getVisibility() != GONE) {
            ...
            measureChildHorizontal(child, params, myWidth, myHeight);
            ...
        }
    }
    // 第二次测量,测量所有不包含垂直依赖关系的child
    views = mSortedVerticalChildren;
    count = views.length;
    ...
    for (int i = 0; i < count; i++) {
        final View child = views[i];
        if (child.getVisibility() != GONE) {
            ...
            measureChild(child, params, myWidth, myHeight);
            ...
        }
    }
    ...
    setMeasuredDimension(width, height);
}

源码如上所示,一共会对child进行两次测量。
1、在测量之前会先准备数据,会根据不同的依赖关系对child进行分类。将不包含水平依赖的child添加进mSortedHorizontalChildren;将不包含垂直依赖的child添加进mSortedVerticalChildren
2、第一次测量,测量所有不包含水平依赖关系的child。
3、第二次测量,测量所有不包含垂直依赖关系的child。

解释一下,这里的水平依赖关系指的是RULES_VERTICAL中定义的一些依赖关系,垂直依赖关系指的是RULES_HORIZONTAL中定义的一些依赖关系。

private static final int[] RULES_VERTICAL = {
        ABOVE, BELOW, ALIGN_BASELINE, ALIGN_TOP, ALIGN_BOTTOM
};

private static final int[] RULES_HORIZONTAL = {
        LEFT_OF, RIGHT_OF, ALIGN_LEFT, ALIGN_RIGHT, START_OF, END_OF, ALIGN_START, ALIGN_END
};

需要注意的是,mSortedHorizontalChildren中添加的是不包含水平依赖关系child,mSortedVerticalChildren中添加的是不包含垂直依赖关系child。如果有的child既没有水平依赖关系,也没有垂直依赖关系,那它就会被分别添加进mSortedHorizontalChildrenmSortedVerticalChildren中,会被测量两次。

经常有人说LinearLayoutRelativeLayout比较的话,推荐使用LinearLayout,因为LinearLayout测量次数少。经过前面的分析我们可以知道,在默认情况下,如果一个child什么额外的属性也不设置,那么在LinearLayout中只会被测量一次,在RelativeLayout中会被测量两次。这就是这个问题的答案了,你下次可以理直气壮的讲原理了。

总结:

综上所述,在RelativeLayout一次测量流程中,RelativeLayout的child最少会经历一次测量(必须的),最多是两次

如果没有设置对应的依赖关系,那么child大概率会被测量两次

相关资料

RelativeLayout

demo:RelativeLayoutTestActivity

系列文章:

从源码角度理解FrameLayout#onMeasure对child的measure调用次数

从源码角度理解LinearLayout#onMeasure对child的measure调用次数

从源码角度理解RelativeLayout#onMeasure对child的measure调用次数

从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数

ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?

相关文章
|
12月前
|
存储 缓存
RecyclerView 动画原理 | 换个姿势看源码(pre-layout)
RecyclerView 动画原理 | 换个姿势看源码(pre-layout)
53 0
|
12月前
|
存储 缓存 索引
RecyclerView 动画原理 | pre-layout,post-layout 与 scrap 缓存的关系
RecyclerView 动画原理 | pre-layout,post-layout 与 scrap 缓存的关系
55 0
|
XML Android开发 数据格式
从源码角度理解LinearLayout#onMeasure对child的measure调用次数
从源码角度理解LinearLayout#onMeasure对child的measure调用次数
|
XML 数据格式
从源码角度理解FrameLayout#onMeasure对child的measure调用次数
从源码角度理解FrameLayout#onMeasure对child的measure调用次数
|
XML 开发工具 Android开发
从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数
从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
RecyclerView#smoothScrollToPosition调用RecyclerView#OnScrollListener的过程
项目中使用到了RecyclerView#smoothScrollToPosition(0)方法让Recyclerview滚动到顶部,同时给Recyclerview设置了监听器RecyclerView.OnScrollListener。
|
Android开发
从源码角度分析Activity、Window和DecorView的关系
前言 最近想出一篇Android事件分发机制的文章,但是根据很多小伙伴反馈在理解Android事件分发机制之前都不是很明白Activity、Window和DecorView之间的关系,导致在学习Android事件分发机制上理解很费劲,本文将从源码角度带你分析Activity、Window和DecorView之间的关系,让你彻彻底底搞明白。
1388 0