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

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

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

今天我们就从最简单的FrameLayout开始学起,看一下它对子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:FrameLayoutTestActivity

<?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.FrameLayoutTestActivity">

    <com.tinytongtong.androidstudy.measure.view.CustomFrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#BCEAC0"
        android:measureAllChildren="true">

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

        <com.tinytongtong.androidstudy.measure.view.CustomTextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:text="match_parent" />

        <com.tinytongtong.androidstudy.measure.view.CustomButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp"
            android:text="wrap_content"
            android:visibility="visible" />

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

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

</LinearLayout>

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

现实效果

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

自身 view1(固定宽高) view2(均match_parent) view3(均match_parent) view4(均wrap_content)
match_parent match_parent M:2,L:1,D:1 M:2,L:1,D:1 M:2,L:1,D:1 M:2,L:1,D:1 M:2,L:1,D:1
wrap_content match_parent M:2,L:1,D:1 M:2,L:1,D:1 M:4,L:1,D:1(二次测量) M:4,L:1,D:1(二次测量) M:2,L:1,D:1
match_parent wrap_content M:2,L:1,D:1 M:2,L:1,D:1 M:4,L:1,D:1(二次测量) M:4,L:1,D:1(二次测量) M:2,L:1,D:1
wrap_content wrap_content M:2,L:1,D:1 M:2,L:1,D:1 M:4,L:1,D:1(二次测量) M:4,L:1,D:1(二次测量) M:2,L:1,D:1

说明:

MonMeasureLonLayoutDonDraw

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

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

观察表格中的内容,我们发现,当FrameLayout有一边是wrap_content,且child的宽高中有match_parent时,FrameLayout会对这种child多执行一次测量流程。也就是FrameLayout执行一次onMeasure,这种child要走两遍onMeasure

源码分析

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();
    // FrameLayout的宽高如果有一个不是match_parent或者固定数值,measureMatchParentChildren的值就是true。
    final boolean measureMatchParentChildren =
            MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
            MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
    mMatchParentChildren.clear();
    // 第一次测量,针对全部的child
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (mMeasureAllChildren || child.getVisibility() != GONE) {
            // 调用child的measure方法,会触发child的onMeasure方法调用
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            ...
            // 将child中,宽或高不少match_parent的,添加进mMatchParentChildren容器中
            if (measureMatchParentChildren) {
                if (lp.width == LayoutParams.MATCH_PARENT ||
                        lp.height == LayoutParams.MATCH_PARENT) {
                    mMatchParentChildren.add(child);
                }
            }
        }
    }

源码如上所示,一共会对child进行两次测量。

第一遍测量所有child(GONE的会忽略掉);

第二遍测量部分child,这部分是有条件的,具体如下:


①FrameLayout中有一边的尺寸不是MeasureSpec.EXACTLY,也就是不是match_parent或者固定数值。

②子view中有宽或者高是LayoutParams.MATCH_PARENT的,也就是match_parent,将这些child添加

③满足条件②的子view超过2个。

此时满足条件②的所有子view会进行二次测量。

总结

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

相关资料

FrameLayout

进入Activity时,为何页面布局内View#onMeasure会被调用两次?

demo:FrameLayoutTestActivity

系列文章:

从源码角度理解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 Android开发 数据格式
从源码角度理解LinearLayout#onMeasure对child的measure调用次数
从源码角度理解LinearLayout#onMeasure对child的measure调用次数
|
XML 开发工具 Android开发
从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数
从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
|
XML 前端开发 数据格式
默认状态下,ViewGroup为什么不走onDraw()?
两种代码相同的自定义ViewGroup,只是改变了在xml中ViewGroup的背景,一个就会调用onDraw而另外一个则不会,那么为什么在不改变ViewGroup的情况下不走onDraw方法呢?那么又该如何解决这个问题??
283 1
|
Android开发
从源码角度分析Activity、Window和DecorView的关系
前言 最近想出一篇Android事件分发机制的文章,但是根据很多小伙伴反馈在理解Android事件分发机制之前都不是很明白Activity、Window和DecorView之间的关系,导致在学习Android事件分发机制上理解很费劲,本文将从源码角度带你分析Activity、Window和DecorView之间的关系,让你彻彻底底搞明白。
1393 0
|
前端开发 Java Android开发
自定义控件View之onMeasure调用时机源码分析
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ 先上测试代码: MainActivity.java import android.
1181 0