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
- Android:线性布局和相对布局的效率对比小实验
- android 线性布局加载的效率不如相对布局
- 线性布局和相对布局小Demo
- Android 线性布局和相对布局
- Android相对布局和线性布局
- Android相对布局和线性布局
- 【Android】线性布局和相对布局的比较
- Android----------线性布局和相对布局的使用
- android线性布局和相对布局的总结
- 线性布局和相对布局的知识点
- Android 布局(一):相对布局和线性布局
- Android 五大布局之线性布局和相对布局
- Android studio布局方式之线性布局和相对布局的结合
- 线性和相对布局的总结
- Android学习.1(线性布局和相对布局)
- android 线性布局LinearLayout 和 相对布局RelativeLayout
- 线性布局和相对布局的一些属性
- 线性布局LinearLayout和相对布局RelativeLayout 之间的比较
- 【Dubbo】初识
- Ubuntu 16.04 lts 下安装mysql + php + nginx + phpmyadmin(图形化mysql管理工具)
- 【C#基础知识点】——vs常用快捷键
- JavaScript逻辑运算符之或(||) 总结
- Mysql单机多实例及主从备份设置
- Android:线性布局和相对布局的效率对比小实验
- 类型系列题目:1. Two Sum 15,3 sum ,16 3sum closet, 18 4sum, 454 4Sum II
- java中List的用法
- Binary Tree Maximum Path Sum 求二叉树的最大路径和
- 修复windows 、Linux双系统无法正确启动
- spark的作业提交流程
- 史上最简单的 MySQL 教程(二)「关系型数据库」
- NYOJ 1272 表达式求值 (字符串处理)
- Anjular简单快速入门