ConstraintLayout,RelativeLayout和LinearLayout的性能对比

        从五年前刚接触Android开发,到初入职场。不管想写什么小demo,或者做什么课程设计,拿到需求,我就开始自己的线性布局之旅了。不管多么复杂的布局,我几乎都能通过各种嵌套来实现。但是,逐渐的,我也听到身边的同事说线性布局嵌套会导致过度绘制,影响性能等等的说法。那时,我还一脸懵逼。确实,那时候,我也只是想着完成功能。

        那么,为什么我想写一篇文章来对比一下这三种布局的性能呢?因为,以前我用线性布局,有同事说相对布局好一些,然后我会适量的使用相对布局。后来,随着约束布局的出现和使用人群日渐增多,我在否定约束布局很久后,终于也没逃过“真香定理”。确实,约束布局她太香了。尝试过一次约束布局后,我开始大量的使用,以至于我最喜欢的线性布局,我都很少去触碰它了。但是,我确实没有自己去验证一下,这三种布局是否在性能上的差别。好,接下来,我就以我的方式去对比一下这三种布局的性能。

一.从需求入手

        首先,我们给自己一个需求。就做一个老生常谈的登录界面吧!很简单的登录界面:

二.性能比较

        首先,说明一下,这里的性能比较是在不嵌套布局的情况下去对比。我们分别用线性布局,相对布局和约束布局去实现上面的登录界面。比较方式:我们在这里也没有去详细的比较绘制的三个流程分别耗时多少,我们就打印一下setContentView的耗时情况。(注:(1)不统计除此启动耗时(2)均为杀死进程后重新启动(3)测试手机为小米Max 2)

        long startTime = System.currentTimeMillis();
        setContentView(R.layout.activity_main_linear);
        long endTime = System.currentTimeMillis();
        long totalTime = endTime-startTime;
        Log.d("TTTT","totalTime:"+totalTime);

1.线性布局

(1)代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edt_1"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="200dp" />

    <EditText
        android:id="@+id/edt_2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:text="login in"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

</LinearLayout>

  (2)耗时情况

线性布局耗时表(单位:ms)
1 2 3 4 5 6 7 8 9 10 平均
130 129 131 130 129 132 131 131 129 129 130

 2.相对布局

(1)代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edt_1"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200dp" />

    <EditText
        android:id="@+id/edt_2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_below="@+id/edt_1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_below="@+id/edt_2"
        android:text="login in"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />

</RelativeLayout>

(2)耗时情况

相对布局耗时表(单位:ms)
1 2 3 4 5 6 7 8 9 10 平均
132 130 130 131 128 129 130 127 129 130 130

3.约束布局

(1)代码 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <EditText
        android:id="@+id/edt_1"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginTop="200dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edt_2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edt_1" />

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:text="login in"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edt_2" />

</androidx.constraintlayout.widget.ConstraintLayout>

(2)耗时情况

约束布局耗时表(单位:ms)
1 2 3 4 5 6 7 8 9 10 平均
160 163 163 162 161 159 168 168 156 158 162

4.比较结果 

        经过上面的数据,我们可以看出,在简单布局下,相对布局和线性布局的耗时情况差不多。但是,约束布局的耗时反而会多了30ms。大家都知道,在不考虑布局嵌套的情况下,相对布局由于会在measure中计算两次,会导致耗时比线性布局稍微多一些。而约束布局是相对布局的进化版,在处理简单布局的时候,耗时确实有所增加。

三.个人观点

        根据自己的实际工作,以及了解到的一些同事对布局的使用情况,说一下个人的几个观点:

(1)在布局简单的情况下,优先使用LinearLayout,其次考虑RelativeLayout和ConstraintLayout。原因:简单布局下,LinearLayout确实简单,且耗时不多,代码量也相对少一些。

(2)在布局比较复杂的情况下,优先使用RelativeLayout和ConstraintLayout,不建议使用LinearLayout各种嵌套。原因:布局过多层次的嵌套,会增加绘制时间。

(3)不必刻意追求使用ConstraintLayout,如果你习惯了使用RelativeLayout。原因:ConstraintLayout和RelativeLayout在处理复杂布局时,我没做过更多的耗时比较。但是,有很多人确实习惯使用RelativeLayout,那么,也不必强行使用约束布局,因为有些人会觉得约束布局会写很多的代码。。。

 

发布了58 篇原创文章 · 获赞 10 · 访问量 17万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章