从源码角度理解LinearLayout#onMeasure对child的measure调用次数
熟悉绘制流程的都知道,ViewGroup
可以决定child的绘制时机以及调用次数。
今天我们就从LinearLayout开始学起,看一下它对子View
的onMeasure
调用次数具体是多少。
简单起见,我们选择进入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.CustomLinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".measure.LinearLayoutTestActivity"><com.tinytongtong.androidstudy.measure.view.CustomSingleViewandroid:layout_width="20dp"android:layout_height="100dp"android:background="@color/colorPrimaryDark" /><com.tinytongtong.androidstudy.measure.view.CustomTextViewandroid: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.CustomButtonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="2"android:text="wrap_content" /><com.tinytongtong.androidstudy.measure.view.CustomImageViewandroid: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_parent
和wrap_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) |
说明:
M
:onMeasure
;L
:onLayout
;D
:onDraw
。
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?
从源码角度理解LinearLayout#onMeasure对child的measure调用次数相关推荐
- 从源码角度理解FrameLayout#onMeasure对child的measure调用次数
熟悉绘制流程的都知道,ViewGroup可以决定child的绘制时机以及调用次数. 今天我们就从最简单的FrameLayout开始学起,看一下它对子View的onMeasure调用次数具体是多少. 简 ...
- 从源码角度理解ConstraintLayout#onMeasure对child的measure调用次数
熟悉绘制流程的都知道,ViewGroup可以决定child的绘制时机以及调用次数. 今天我们简单看下较为复杂的ConstraintLayout,看一下它对子View的onMeasure调用次数具体是多 ...
- 从源码角度理解 FragmentTransaction实现
谈到fragment的使用,肯定绕不过FragmentTransaction事务,对fragment的操作必定用到它,其提供show,hide,add,remove,replace等常用的fragme ...
- 对应到对象 数据库驼峰_从源码角度理解Mybatis字段映射(一) - 驼峰式命名
凯伦说,公众号ID: KailunTalk,努力写出最优质的技术文章,欢迎关注探讨. 在上篇博客-[JDBC] 处理ResultSet,构建Java对象中提到,我们需要分析Mybatis在转换Resu ...
- linux线程handler,Handler从源码角度理解
上一个文章讲解了Handler的基本使用,同时也有一些问题没有解决,本篇带你从源码的角度理解. 首先让我们来看看Handler的构造方法: image.png 我靠这么多的构造方法啊,我们上一篇只用了 ...
- java comparator 降序排序_【转】java comparator 升序、降序、倒序从源码角度理解
原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境 jdk:1.7+ 前言 之前我写过关于comparator的理解,但 ...
- Android-带你从源码角度理解SharedPreferences存储原理
SP的特点以及基本使用方式 SharedPreferences因非常适合存储较小键值集合数据且使用非常简单的特点,而受到广大程序员们热爱. SP使用非常简单: //读操作 Context contex ...
- jdbc mysql 源码_【JDBC系列】从源码角度理解JDBC和Mysql的预编译特性
背景 最近因为工作调整的关系,都在和数据库打交道,增加了许多和JDBC亲密接触的机会,其实我们用的是Mybatis啦.知其然,知其所以然,是我们工程师童鞋们应该追求的事情,能够帮助你更好的理解这个技术 ...
- 从源码角度深入理解Toast
Toast这个东西我们在开发中经常用到,使用也很简单,一行代码就能搞定: 1: Toast.makeText(this, "333", Toast.LENGTH_LONG).sho ...
最新文章
- Android App的启动过程
- pyqt tableview mysql_当数据库中添加了一个新条目时,如何在PyQt中更新TableView?
- Grails 复用查询条件并分页
- javascript 制作的美化select,利用cookie保存选择
- OpenCV使用GDAL读取地理空间栅格文件
- 74 计算机图形学开源处理库
- 是否同一棵二叉搜索树(c语言实现)
- spray.json.JsonParser$ParsingException: Unexpected end-of-input at input index
- LOAM_velodyne学习(三)
- 线程停止继续_晓龙吊打面试官系列: 如何优雅的停止一个线程
- shell脚本spawn_如何使用child_process.spawn将Python / Ruby / PHP Shell脚本与Node.js集成
- 基于Java的2048小游戏设计
- Mysql基础增删改查语句
- python简易语音助手
- out在matlab中,在仿真模型中添加一个输出端口模块(Out模块),能够将结果输出到MATLAB工作空间中。...
- python元祖封包_python的封包与解包
- 一、【s3c2440移植linux-3.5】移植准备
- 模拟电路--同相放大器和反相放大器的选择
- matlab nargin的意思
- AngularJS + RequireJS