熟悉绘制流程的都知道,ViewGroup可以决定child的绘制时机以及调用次数。
今天我们简单看下较为复杂的ConstraintLayout,看一下它对子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 sdk版本30
由于ConstraintLayout的功能强大,导致其绘制流程相对复杂,我无法在很短的篇幅中讲到所有的绘制关键节点,那就退而求其次,以一些比较典型的用法来观测ConstraintLayout的绘制调用栈,让大家直观的感受下ConstraintLayout中绘制相关的核心类。
xml布局:里面的自定义View都只是添加了log。
demo:ConstraintLayoutTest1Activity
<?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.ConstraintLayoutTest1Activity">
<com.tinytongtong.androidstudy.measure.view.CustomConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.tinytongtong.androidstudy.measure.view.CustomSingleView
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.tinytongtong.androidstudy.measure.view.CustomTextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="我是文本我是文本我是文本"
android:textColor="#FDA413"
android:textSize="12sp"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
ap

本文详细分析了ConstraintLayout中child在onMeasure方法的调用次数,指出一次测量流程中通常仅执行一次,即便功能丰富。通过示例和源码剖析,揭示了ConstraintLayout如何优化测量过程并避免重复测量。

704

被折叠的 条评论
为什么被折叠?



