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

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

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

今天我们就从LinearLayout开始学起,看一下它对子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观测现象

时机:进入页面;


android:orientation="vertical"

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

demo:LinearLayoutTestActivity

<?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"
    android:orientation="vertical"
    tools:context=".measure.LinearLayoutTestActivity">

    <com.tinytongtong.androidstudy.measure.view.CustomLinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context=".measure.LinearLayoutTestActivity">

        <com.tinytongtong.androidstudy.measure.view.CustomSingleView
            android:layout_width="20dp"
            android:layout_height="100dp"
            android:background="@color/colorPrimaryDark" />

        <com.tinytongtong.androidstudy.measure.view.CustomTextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#99dddddd"
            android:gravity="center"
            android:text="match_parent" />

        <com.tinytongtong.androidstudy.measure.view.CustomButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="wrap_content" />

        <com.tinytongtong.androidstudy.measure.view.CustomImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@drawable/ic_launcher"
            android:contentDescription="wrap_content" />

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

</LinearLayout>

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

现实效果

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

自身 view1(固定宽高,无weight) view2(w:match_parent,h:0,weight:1) view3(w:match_parent,h:wrap_content,weight:2) view4(w:wrap_content,h:wrap_content,weight:3) 备注
match_parent match_parent M:2,L:1,D:0(默认不参与onDraw) M:2,L:1,D:1(只参与第一次onMeasure) M:2,L:1,D:1(不参与第一次onMeasure,参与第二次onMeasure) M:4,L:1,D:2(参与第一、二次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure) height为0且weight>0时,不参与第一次onMeasure。weight>0时,会参与第二次onMeasure。
match_parent wrap_content M:2,L:1,D:0 M:2,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) 有权重的,都会参与第二次onMeasure。
wrap_content match_parent M:2,L:1,D:0 M:2,L:1,D:1(只参与第一次onMeasure) M:4,L:1,D:1(不参与第一次onMeasure,参与第二、三次onMeasure) M:6,L:1,D:2(参与第一、二、三次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure)
wrap_content wrap_content M:2,L:1,D:0 M:2,L:1,D:1(只参与第一次onMeasure) M:6,L:1,D:1(参与第一、二、三次onMeasure) M:6,L:1,D:2(参与第一、二、三次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure)

说明:

MonMeasureLonLayoutDonDraw

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

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

观察表格中的内容,我们发现在一次测量流程中,LinearLayout的child,最少是一次测量最多是三次,weight和尺寸都会有有影响。

LinearLayout#measureVertical源码分析

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

void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
    ...
    // See how tall everyone is. Also remember max width.
    // 第一次测量,LinearLayout的高度固定(MeasureSpec.EXACTLY),同时child的高度为0且child的权重大于0,这些child不参与第一次测量,其余child全部参与测量。
    for (int i = 0; i < count; ++i) {
        final View child = getVirtualChildAt(i);
        ...

        final boolean useExcessSpace = lp.height == 0 && lp.weight > 0;
        if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) {
            ...
        } else {
            ...
            measureChildBeforeLayout(child, i, widthMeasureSpec, 0,
                    heightMeasureSpec, usedHeight);
            ...
            // 关注宽度
            if (widthMode != MeasureSpec.EXACTLY && lp.width == LayoutParams.MATCH_PARENT) {
                ...
                matchWidth = true;
                ...
            }
            ...
        }
        ...
    }
    ...

    // 第二次测量,针对有权重的child进行测量,第一次测量跳过的View,会在这里进行测量。
    if (skippedMeasure
            || ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) {
        ...
        for (int i = 0; i < count; ++i) {
            final View child = getVirtualChildAt(i);
            ...
            if (childWeight > 0) {
                ...
                final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        Math.max(0, childHeight), MeasureSpec.EXACTLY);
                final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                        mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin,
                        lp.width);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
                ...
            }
            ...
        }
        ...
    } else {
        ...
    }
    ...
    setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
            heightSizeAndState);
    
    // 第三次测量,关注width的测量。如果widthMode != MeasureSpec.EXACTLY,且有child的宽度是match_parent,
    // 则执行forceUniformWidth方法,开启for循环,将宽度是match_parent的view重新measure一遍。
    if (matchWidth) {
        forceUniformWidth(count, heightMeasureSpec);
    }
}

LinearLayout#measureVertical总结:

android:orientation="vertical"时,LinearLayout的child,最少会经过一次测量最多会经历三次测量


1、第一次测量,LinearLayout的高度固定(MeasureSpec.EXACTLY),同时child的高度为0且child的权重大于0,这些child不参与第一次测量,其余child全部参与测量。

2、第二次测量,针对有权重的child进行测量,第一次测量跳过的View,会在这里进行测量。

3、第三次测量,关注width的测量。如果widthMode != MeasureSpec.EXACTLY,且有child的宽度是match_parent,则执行forceUniformWidth方法,开启for循环,将宽度是match_parent的view重新measure一遍。

感兴趣的同学可以对着表格中的log数据,尝试着跟源码中的场景对应起来。这里就不再赘述了。

LinearLayout#measureHorizontal

接下来我们分析下android:orientation="vertical"的场景。布局文件就不贴了,直接看对应的log表格:

自身 view1(固定宽高,无weight) view2(w:0,h:match_parent,weight:1) view3(w:wrap_content,h:match_parent,weight:2) view4(w:wrap_content,h:wrap_content,weight:3) 备注
match_parent match_parent M:2,L:1,D:0(默认不参与onDraw) M:2,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)
match_parent wrap_content M:2,L:1,D:0 M:2,L:1,D:1 M:6,L:1,D:1(参与第一、二、三次onMeasure) M:6,L:1,D:2(参与第一、二、三次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure)
wrap_content match_parent M:2,L:1,D:0 M:2,L:1,D:1 M:4,L:1,D:1(参与第一、二次onMeasure) M:4,L:1,D:2(参与第一、二次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure)
wrap_content wrap_content M:2,L:1,D:0 M:2,L:1,D:1 M:6,L:1,D:1(参与第一、二、三次onMeasure) M:6,L:1,D:2(参与第一、二、三次onMeasure,参与第一、二次onDraw) M:4,L:1,D:1(参与第一、二次onMeasure)

情况跟水平方向类似,一个测量流程中,child也是最少一次测量,最多三次测量。

LinearLayout#measureHorizontal源码分析

具体看下LinearLayout#measureHorizontal源码:

void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
    ...
    // See how wide everyone is. Also remember max height.
    // 第一次测量,同时满足下面几个条件的child不参与第一次测量,否则就参与第一次测量。
    // ①LinearLayout的宽度固定(MeasureSpec.EXACTLY);
    // ②child的宽度为0且child的权重大于0;
    // ③child设置了baselineAligned属性。
    for (int i = 0; i < count; ++i) {
        final View child = getVirtualChildAt(i);
        ...
        if (widthMode == MeasureSpec.EXACTLY && useExcessSpace) {
            ...
            if (baselineAligned) {
                ...
                child.measure(freeWidthSpec, freeHeightSpec);
            } else {
                skippedMeasure = true;
            }
        } else {
            ...
            measureChildBeforeLayout(child, i, widthMeasureSpec, usedWidth,
                    heightMeasureSpec, 0);

            final int childWidth = child.getMeasuredWidth();
            if (useExcessSpace) {
                // Restore the original width and record how much space
                // we've allocated to excess-only children so that we can
                // match the behavior of EXACTLY measurement.
                lp.width = 0;
                usedExcessSpace += childWidth;
            }
            ...
            // 关注高度
            if (useLargestChild) {
                largestChildWidth = Math.max(childWidth, largestChildWidth);
            }
        }
        ...
    }
    ...
    // 第二次测量,针对有权重的child进行测量,第一次测量跳过的View,会在这里进行测量。
    if (skippedMeasure
            || ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) {
        ...
        for (int i = 0; i < count; ++i) {
            final View child = getVirtualChildAt(i);
            ...
            if (childWeight > 0) {
                ...
                final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                        Math.max(0, childWidth), MeasureSpec.EXACTLY);
                final int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                        mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin,
                        lp.height);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
                ...
            }
            ...
        }
        ...
    } else {
        ...
    }
    ...
    setMeasuredDimension(widthSizeAndState | (childState&MEASURED_STATE_MASK),
            resolveSizeAndState(maxHeight, heightMeasureSpec,
                    (childState<<MEASURED_HEIGHT_STATE_SHIFT)));
    // 第三次测量,关注height的测量。如果heightMode != MeasureSpec.EXACTLY,且有child的高度是match_parent,
    // 则forceUniformHeight方法,开启for循环,将高度是match_parent的view重新measure一遍。
    if (matchHeight) {
        forceUniformHeight(count, widthMeasureSpec);
    }
}

LinearLayout#measureVertical总结:

android:orientation="vertical"时,LinearLayout的child,最少会经过一次测量最多会经历三次测量


1、第一次测量,同时满足下面几个条件的child不参与第一次测量,否则就参与第一次测量。

①LinearLayout的宽度固定(MeasureSpec.EXACTLY);

②child的宽度为0且child的权重大于0;

③child设置了baselineAligned属性。

2、第二次测量,针对有权重的child进行测量,第一次测量跳过的View,会在这里进行测量。

3、第三次测量,关注height的测量。如果heightMode != MeasureSpec.EXACTLY,且有child的高度是match_parent,则forceUniformHeight方法,开启for循环,将高度是match_parent的view重新measure一遍。

总结

总的来说,一次测量流程中,LinearLayout的child最少进行一次测量(必须的)最多进行三次测量

第一次测量基本上针对所有的child(有特例,看上面的解析),第二次测量针对有权重的child,第三次测量针对另一个方向上、尺寸是match_parent的child。

相关资料

LinearLayout

demo:LinearLayoutTestActivity

系列文章:

从源码角度理解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)
55 0
|
12月前
|
存储 缓存 索引
RecyclerView 动画原理 | pre-layout,post-layout 与 scrap 缓存的关系
RecyclerView 动画原理 | pre-layout,post-layout 与 scrap 缓存的关系
57 0
|
XML Android开发 数据格式
从源码角度理解RelativeLayout#onMeasure对child的measure调用次数
从源码角度理解RelativeLayout#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?
调用View#requestLayout后,哪些View会被影响?
调用View#requestLayout后,哪些View会被影响?
|
Android开发
图+源码,读懂View的Measure方法
本篇是 读懂View 系列的第二篇文章,本文将给大家正式开始讲解View绘制的三大方法,本篇将讲述第一个方法—— Measure 方法。
图+源码,读懂View的Measure方法
|
Android开发
从源码角度分析Activity、Window和DecorView的关系
前言 最近想出一篇Android事件分发机制的文章,但是根据很多小伙伴反馈在理解Android事件分发机制之前都不是很明白Activity、Window和DecorView之间的关系,导致在学习Android事件分发机制上理解很费劲,本文将从源码角度带你分析Activity、Window和DecorView之间的关系,让你彻彻底底搞明白。
1393 0