Android:线性布局和相对布局的效率对比小实验

来源:互联网 发布:全职之烽火知韩 编辑:程序博客网 时间:2024/05/20 00:10

前言

不久前在项目中写布局用了大量的相对布局,但被提出说相对布局效率慢,于是就想验证下是否确实如此。


实验方法

最准确的分析方法还是看源码,限于篇幅加上本人水平有限这里就不贴了。文章想通过程序执行时的现象进行参考,对比效率。大家知道几种布局最主要的实现方法有onMeasure(),onLayout()等,所以就想从这几个方法里入手,通过执行时间来进行对比(这样只是相对比较,实际总时间肯定大于这个时间)


自定义相对布局和线性布局

首先要自定义两个布局类,在onMeasure()打印出时间节点

public class MyLinearLayout extends LinearLayout {    public MyLinearLayout(Context context) {        super(context);    }    public MyLinearLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        Log.e("layout","before onMeasure");        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        Log.e("layout","after onMeasure");    }}

public class MyRelativeLayout extends RelativeLayout {    public MyRelativeLayout(Context context) {        super(context);    }    public MyRelativeLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        Log.e("layout", "before onMeasure");        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        Log.e("layout", "after onMeasure");    }}

一.XML中为单个Layout的情况下

<?xml version="1.0" encoding="utf-8"?><com.example.layouttest.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout></com.example.layouttest.MyLinearLayout>

线性布局:方法平均时间(10次):34.6ms

相对布局:方法平均时间(10次):35.3ms


二.XML中有十个Layout(非嵌套)

<?xml version="1.0" encoding="utf-8"?><com.example.layouttest.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout>    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </com.example.layouttest.MyLinearLayout></com.example.layouttest.MyLinearLayout>

线性布局:方法平均时间(10次):39.1ms

相对布局:方法平均时间(10次):41.7ms


三.XML中有十个Layout(嵌套)

<?xml version="1.0" encoding="utf-8"?><com.example.layouttest.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">    <com.example.layouttest.MyLinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />        <com.example.layouttest.MyLinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Hello World!" />            <com.example.layouttest.MyLinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:orientation="vertical">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="Hello World!" />                <com.example.layouttest.MyLinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:orientation="vertical">                    <TextView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="Hello World!" />                    <com.example.layouttest.MyLinearLayout                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:orientation="vertical">                        <TextView                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:text="Hello World!" />                        <com.example.layouttest.MyLinearLayout                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:orientation="vertical">                            <TextView                                android:layout_width="wrap_content"                                android:layout_height="wrap_content"                                android:text="Hello World!" />                            <com.example.layouttest.MyLinearLayout                                android:layout_width="wrap_content"                                android:layout_height="wrap_content"                                android:orientation="vertical">                                <TextView                                    android:layout_width="wrap_content"                                    android:layout_height="wrap_content"                                    android:text="Hello World!" />                                <com.example.layouttest.MyLinearLayout                                    android:layout_width="wrap_content"                                    android:layout_height="wrap_content"                                    android:orientation="vertical">                                    <TextView                                        android:layout_width="wrap_content"                                        android:layout_height="wrap_content"                                        android:text="Hello World!" />                                    <com.example.layouttest.MyLinearLayout                                        android:layout_width="wrap_content"                                        android:layout_height="wrap_content"                                        android:orientation="vertical">                                        <TextView                                            android:layout_width="wrap_content"                                            android:layout_height="wrap_content"                                            android:text="Hello World!" />                                        <com.example.layouttest.MyLinearLayout                                            android:layout_width="wrap_content"                                            android:layout_height="wrap_content"                                            android:orientation="vertical">                                            <TextView                                                android:layout_width="wrap_content"                                                android:layout_height="wrap_content"                                                android:text="Hello World!" />                                        </com.example.layouttest.MyLinearLayout>                                    </com.example.layouttest.MyLinearLayout>                                </com.example.layouttest.MyLinearLayout>                            </com.example.layouttest.MyLinearLayout>                        </com.example.layouttest.MyLinearLayout>                    </com.example.layouttest.MyLinearLayout>                </com.example.layouttest.MyLinearLayout>            </com.example.layouttest.MyLinearLayout>        </com.example.layouttest.MyLinearLayout>    </com.example.layouttest.MyLinearLayout></com.example.layouttest.MyLinearLayout>

线性布局:方法平均时间(10次):37.8ms

相对布局:方法平均时间(10次):205ms


可见在单个布局或者没嵌套的情况下,两种布局效率几乎可以忽略。在嵌套的情况下,嵌套层级越多,相对布局执行的时间会更长。不过相比之下我还是更喜欢用相对布局,更灵活,更全能,而且的话,我认为在优化这个问题之前,程序里有别的更应该优化的地方。

0 0