Android
开发中,性能优化策略十分重要布局性能的好坏 主要影响 :Android
应用中的页面显示速度
布局影响Android
性能的实质:页面的测量 & 绘制时间
1个页面通过递归 完成测量 & 绘制过程 =
measure
、layout
过程
针对 页面布局的性能、层级、测量绘制时间 进行优化,从而提高 Android
应用中的页面显示速度
FrameLayout
、LinearLayout
RelativeLayout
即 布局过程需消耗更多性能(
CPU
资源 & 时间)
注:
- 嵌套所耗费的性能 > 单个布局本身耗费的性能
- 即 完成需求时:宁选择 1个耗费性能高的布局,也不采用嵌套多个耗费性能低的布局
& 合适选择布局类型配合
标签使用,可优化 加载布局文件时的资源消耗
// 使用说明:
// 1. 作为被引用布局A的根标签
// 2. 当其他布局通过标签引用布局A时,布局A中的标签内容(根节点)会被去掉,在里存放的是布局A中的标签内容(根节点)的子标签(即子节点),以此减少布局文件的层次
/**
* 实例说明:在上述例子,在布局B中 通过
* 此时:布局层级为 = RelativeLayout ->> Button
* —>> RelativeLayout ->> Button
* ->> TextView
* 现在使用
* 在引用布局C时,布局C中的
* 即
// 被引用的公共部分:布局C = layout_c.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
</merge>
// 布局B:layout_b.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/Button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp_10" />
<include layout="@layout/layout_c.xml" />
</RelativeLayout>
UI
效果时,尽可能选择1个功能复杂的布局(如RelativeLayout
)完成,而不要选择多个功能简单的布局(如LinerLayout
)通过嵌套完成
作用
实现 布局模块化,即 提取布局中的公共部分 供其他布局共用
具体使用
// 使用说明:
// a. 通过标签引入抽取的公共部分布局C
// b. 标签所需属性 = 公共部分的layout属性,作用 = 指定需引入、包含的布局文件
// 实例说明:抽取 布局A、B中的公共部分布局C & 放入到布局B中使用
/**
* 布局B:layout_b.xml
*/
android:layout_height=“match_parent” >
<Button
android:id="@+id/Button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp_10" />
// 通过<include>标签引入抽取的公共部分布局C
// <include>标签所需属性 = 公共部分的layout属性,作用 = 指定需引入、包含的布局文件
<include layout="@layout/layout_c.xml" />
</RelativeLayout>
/**
* 公共部分的布局C:layout_c.xml
*/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
</RelativeLayout>
主要优化方案:使用 布局标签
& 尽可能少用布局属性 wrap_content
注:属 轻量级
View
、不占用显示 & 位置
如:进度显示布局、信息出错出现的提示布局等
// 使用说明:
// 1. 先设置好预显示的布局
// 2. 在其他布局通过标签引入外部布局(类似);注:此时该布局还未被加载显示
// 3. 只有当ViewStub被设置为可见 / 调用了ViewStub.inflate()时,ViewStub所指向的布局文件才会被inflate 、实例化,最终 显示指向的布局
/**
* 实例说明:在布局A中引入布局B,只有在特定时刻C中才显示
*/
// 步骤1:先设置好预显示的布局B = layout_b.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>
</RelativeLayout>
// 步骤2:在布局A通过<ViewStub>标签引入布局B(类似<include>);注:此时该布局还未被加载显示
// 布局A:layout_a.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/Button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp_10" />
<ViewStub
android:id="@+id/Blayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/layout_b" />
</RelativeLayout>
// 步骤3:只有当ViewStub被设置为可见 / 调用了ViewStub.inflate()时,ViewStub所指向的布局文件才会被inflate 、实例化,最终 显示<ViewStub>指向的布局
ViewStub stub = (ViewStub) findViewById(R.id.Blayout);
stub.inflate();
// 特别注意
// 1. ViewStub中的layout布局不能使用merge标签,否则会报错
// 2. ViewStub的inflate只能执行一次,显示了之后,就不能再使用ViewStub控制它了
// 3. 与View.setVisible(View.Gone)的区别:View 的可见性设置为 gone 后,在inflate 时,该View 及其子View依然会被解析;而使用ViewStub就能避免解析其中指定的布局文件,从而节省布局文件的解析时间 & 内存的占用
布局属性 wrap_content
会增加布局测量时计算成本,应尽可能少用
在已知宽高为固定值时,不使用
wrap_content
至此,关于布局优化的方案讲解完毕
此处主要介绍 常用的:
hierarchy viewer
、Lint
、Systrace
简介Android Studio
提供的UI性能检测工具。
作用
可视化获得UI布局设计结构 & 各种属性信息,帮助我们优化布局设计
即 :方便查看
Activity
布局,各个View
的属性、布局测量-布局-绘制的时间
Android Studio
提供的 代码扫描分析工具
- 该过程不需手写测试用例
Lint
发现的每个问题都有描述信息 & 等级(和测试发现 bug 很相似),可方便定位问题 & 按照严重程度进行解决
Android 4.1
以上版本提供的性能数据采样 & 分析工具Android
系统各个组件随着时间的运行状态 & 提供解决方案
- 收集 等运行信息,从而帮助开发者更直观地分析系统瓶颈,改进性能
检测范围包括:Android
关键子系统(如WindowManagerService
等Framework
部分关键模块)、服务、View系统- 功能包括:跟踪系统的
I/O
操作、内核工作队列、CPU
负载等,在 UI 显示性能分析上提供很好的数据,特别是在动画播放不流畅、渲染卡等问题上
Android
性能优化中的 布局优化