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

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

确切的说,是刚打开Activity时,页面中的View的onMeasure最少会被调用两次。这是为什么呢?接下来我们以LinearLayout为例进行分析。

系列文章:

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

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

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

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

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

自定义LinearLayout验证

先自定义一个LinearLayout:CustomLinearLayout,主要是添加了log打印:

CustomLinearLayout打印log

public class CustomLinearLayout extends LinearLayout {
    private static final String TAG = "CustomLayout-Linear";

    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.e(TAG, String.format("onMeasure widthSpecSize:%s, widthSpecMode:%s, heightSpecSize:%s, heightSpecMode:%s",
                widthSpecSize, widthSpecMode, heightSpecSize, heightSpecMode));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e(TAG, String.format("onLayout changed:%s, l:%s, t:%s, r:%s, b:%s",
                changed, l, t, r, b));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw");
    }
}

简单的xml文件,宽高都是match_parent

接着看下Activity对应的布局文件,布局文件很简单,就一个自定义LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<com.tinytongtong.androidstudy.measure.view.CustomLinearLayout 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.MeasureInvokeTestActivity">

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

查看log

最后启动Activity,我们看下对应的log:

E/CustomLayout-Linear: onMeasure widthSpecSize:1080, widthSpecMode:1073741824, heightSpecSize:1823, heightSpecMode:1073741824
E/CustomLayout-Linear: onMeasure widthSpecSize:1080, widthSpecMode:1073741824, heightSpecSize:1823, heightSpecMode:1073741824
E/CustomLayout-Linear: onLayout changed:true, l:0, t:0, r:1080, b:1823

如上所示,自定义LinearLayout的onMeasure方法确实是调用了两次。

通过debug分析原理

我们在CustomLinearLayout#onMeasure方法中添加一个debug断点,看下实际的调用链。

debug结果:基于api30
第一次调用onMeasure:
在这里插入图片描述
从图中可以清晰的看出,ViewRootImpl#performMeasure方法是在ViewRootImpl的2228行调用。调用链如下:

--> ViewRootImpl#performTraversals
--> ViewRootImpl#measureHierarchy; 2486行调用
--> ViewRootImpl#performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 2228行调用
--> View#measure; 开始执行页面绘制流程
--> View#onMeasure; 从根View开始,从上往下,依次执行依次measure流程。

具体源码如下:ViewRootImpl#performTraversals方法中,2486行

private void performTraversals() {
    ...
    // Ask host how big it wants to be
    windowSizeMayChange |= measureHierarchy(host, lp, res,
            desiredWindowWidth, desiredWindowHeight);
    ...
}

4、第二次调用onMeasure
在这里插入图片描述

调用链如下:

--> ViewRootImpl#performTraversals
--> ViewRootImpl#performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 2280行调用
--> View#measure; 开始执行页面绘制流程
--> View#onMeasure; 从根View开始,从上往下,依次执行依次measure流程。

具体代码如下:

private void performTraversals() {
    ...
    // Ask host how big it wants to be
    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    ...
}

总体来说,两次测量都是ViewRootImpl#performTraversals方法中触发的,第一次是在2486行调用ViewRootImpl#measureHierarchy方法,第二次是在2280行调用ViewRootImpl#performMeasure(childWidthMeasureSpec, childHeightMeasureSpec)

这两次调用各自是什么意思呢?
第一次是在2486行调用ViewRootImpl#measureHierarchy方法,这一步骤的测量ViewTree是为了确定RootView的尺寸从而在此步骤确定Window尺寸

第二次是在2280行调用ViewRootImpl#performMeasure(childWidthMeasureSpec, childHeightMeasureSpec)方法,是在确定了window尺寸的基础上,对RootView进行再次测量,本次就可确定ViewTree中各部分的尺寸。

细心的同学可能已经发现了,我们demo中Activity布局的宽高,写的都是match_parent,属于测量中最简单的场景,此时RootView的宽高是不会超出window尺寸的。如果我们修改一下我们布局的宽高,那么测量流程就不止两次了。感兴趣的同学可以研究下,具体参见Android 一个困惑很久的问题:onMeasure() 为什么会执行多次?

总结

在一个简单的Activity中,写一个简单的布局,宽高都是match_parent。当打开Activity时,从RootView开始从上往下绘制流程(onMeasure方法)最少执行两次

相关资料

Android 一个困惑很久的问题:onMeasure() 为什么会执行多次?

demo地址

系列文章:

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

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

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

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

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

相关文章
|
SQL 人工智能 Dart
Android Studio的插件生态非常丰富
Android Studio的插件生态非常丰富
721 1
|
Android开发
Android 13 平板Taskbar加载流程
Android 13 平板Taskbar加载流程
2053 0
|
XML Java Maven
Maven资源拷贝插件和常用依赖
Maven资源拷贝插件和常用依赖
491 0
|
存储 Java C++
JVM内存模型和结构详解(五大模型图解)
JVM内存模型和结构详解(五大模型图解)
|
12月前
|
安全 API C语言
Python程序的安全逆向(关于我的OPENAI的APIkey是如何被盗的)
本文介绍了如何使用C语言编写一个简单的文件加解密程序,并讨论了如何为编译后的软件添加图标。此外,文章还探讨了Python的.pyc、.pyd等文件的原理,以及如何生成和使用.pyd文件来增强代码的安全性。通过视频和教程,作者详细讲解了生成.pyd文件的过程,并分享了逆向分析.pyd文件的方法。最后,文章提到可以通过定制Python解释器来进一步保护源代码。
303 6
|
分布式计算 API Apache
Dask与Apache Spark的对比
【8月更文挑战第10天】随着数据量激增,高效处理成为关键。本文对比了Python领域的两大工具——Dask与Apache Spark。Dask提供类似NumPy和Pandas的API,适用于中小规模数据;而Spark作为内存型处理引擎,擅长超大规模数据处理。我们通过代码实例展示了两者的使用方式,并分析了它们在性能、API及生态系统方面的异同。无论您追求易用性还是高性能,都能从中找到合适的选择。
|
域名解析 存储 网络安全
WordPress外贸建站教程
这篇WordPress外贸建站教程是以实操形式写给没有任何建站基础的新手,不管你是不是技术小白,都可以轻松学会如何使用WordPress来自己建立一个实用的外贸网站,而不需要深入了解复杂的代码编程。梳理了WordPress外贸建站主要步骤,从最初的成本分析开始,然后逐步介绍域名选择和注册、虚拟主机选择、建站程序安装等关键步骤。
778 3
|
XML 前端开发 Java
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
349 1
|
存储
QComboBox的Item项携带(存储)一个或多个数据
1.携带(存储)一个数据:addItem(const QString &text, const QVariant &userData = QVariant()) 2.携带(存储)多个数据:setModel(QAbstractItemModel *model)
356 0
|
前端开发 定位技术
百度地图开发自定义信息窗口openInfoWindow样式的解决方案
百度地图开发自定义信息窗口openInfoWindow样式的解决方案
1877 0