在 Android开发中,性能优化策略十分重要
本文主要讲解性能优化中的布局优化,希望你们会喜欢。
目录

示意图

1. 影响的性能

布局性能的好坏 主要影响 :Android应用中的页面显示速度

2. 如何影响性能

布局影响Android性能的实质:页面的测量 & 绘制时间

1个页面通过递归 完成测量 & 绘制过程 = measure、layout 过程
3. 优化思路

优化方向:布局性能、布局层级、布局复用性 和 测量 & 绘制时间
具体如下
示意图

针对 页面布局的性能、层级、测量绘制时间 进行优化,从而提高 Android应用中的页面显示速度

4. 具体优化方案

具体如下
示意图

下面,我将详细分析每种优化方案
4.1 选择 耗费性能较少的布局

性能耗费低的布局 = 功能简单 = FrameLayout、LinearLayout
性能耗费高的布局 = 功能复杂 = RelativeLayout
即 布局过程需消耗更多性能(CPU资源 & 时间)

注:
1. 嵌套所耗费的性能 > 单个布局本身耗费的性能
2. 即 完成需求时:宁选择 1个耗费性能高的布局,也不采用嵌套多个耗费性能低的布局
4.2 减少布局的层级(嵌套)

原理:布局层级少 ->> 绘制的工作量少 ->> 绘制速度快 ->> 性能提高
优化方式:使用布局标签<merge> & 合适选择布局类型
4.2.1 使用布局标签

作用
减少 布局层级

配合<include>标签使用,可优化 加载布局文件时的资源消耗
具体使用

// 使用说明:
// 1. <merge>作为被引用布局A的根标签
// 2. 当其他布局通过<include>标签引用布局A时,布局A中的<merge>标签内容(根节点)会被去掉,在<include>里存放的是布局A中的<merge>标签内容(根节点)的子标签(即子节点),以此减少布局文件的层次

/**
* 实例说明:在上述例子,在布局B中 通过<include>标签引用布局C
* 此时:布局层级为 = RelativeLayout ->> Button
* —>> RelativeLayout ->> Button
* ->> TextView
* 现在使用<merge>优化:将 被引用布局C根标签 的RelativeLayout 改为 <merge>
* 在引用布局C时,布局C中的<merge>标签内容(根节点)会被去掉,在<include>里存放的是布局C中的<merge>标签内容(根节点)的子标签(即子节点)
* 即 <include>里存放的是:<Button>、<TextView>
* 此时布局层级为 = RelativeLayout ->> Button
* ->> Button
* ->> TextView
* 即 已去掉之前无意义、多余的<RelativeLayout>
*/

// 被引用的公共部分:布局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" />

4.2.2 合适选择布局类型

通过合理选择布局类型,从而减少嵌套
即:完成 复杂的UI效果时,尽可能选择1个功能复杂的布局(如RelativeLayout)完成,而不要选择多个功能简单的布局(如LinerLayout)通过嵌套完成
4.3 提高 布局 的复用性

原因
提取布局间的公共部分,通过提高布局的复用性从而减少测量 & 绘制时间
优化方案
使用 布局标签 <include>
4.3.1 使用 布局标签

作用
实现 布局模块化,即 提取布局中的公共部分 供其他布局共用

具体使用

// 使用说明:
// a. 通过<include>标签引入抽取的公共部分布局C
// b. <include>标签所需属性 = 公共部分的layout属性,作用 = 指定需引入、包含的布局文件

// 实例说明:抽取 布局A、B中的公共部分布局C & 放入到布局B中使用

/**
* 布局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="matc www.douniu178.com h_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"www.dfgj157.com >

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"www.dfgjyl.cn/ />

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10"/>

</RelativeLayout>
4.4 减少初次测量 & 绘制时间

主要优化方案:使用 布局标签<ViewStub> & 尽可能少用布局属性 wrap_content

4.4.1 使用 布局标签

作用
按需加载 外部引入的布局

注:属 轻量级View、不占用显示 & 位置
应用场景
引入 只在特殊情况下才显示的布局(即 默认不显示)

如:进度显示布局、信息出错出现的提示布局等
具体使用

// 使用说明:
// 1. 先设置好预显示的布局
// 2. 在其他布局通过<ViewStub>标签引入外部布局(类似<include>);注:此时该布局还未被加载显示
// 3. 只有当ViewStub被设置为可见 / 调用了ViewStub.inflate()时,ViewStub所指向的布局文件才会被inflate 、实例化,最终 显示<ViewStub>指向的布局

/**
* 实例说明:在布局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_10www.furong157.com"/>

</RelativeLayout>

// 步骤2:在布局A通过<ViewStub>标签引入布局B(类似<include>);注:此时该布局还未被加载显示
// 布局A:layout_a.xml
<?xml version="1.0www.2636666.cn" 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)www.huarenyl.cn 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就能避免解析其中指定的布局文件,从而节省布局文件的解析时间 & 内存的占用
4.4.2 尽可能少用布局属性 wrap_content

布局属性 wrap_content 会增加布局测量时计算成本,应尽可能少用

在已知宽高为固定值时,不使用wrap_content
总结

示意图

至此,关于布局优化的方案讲解完毕

5. 布局调优工具

背景
尽管已经注意到上述的优化策略,但实际开发中难免还是会出现布局性能的问题
解决方案
使用 布局调优工具
此处主要介绍 常用的:hierarchy viewer、Lint、Systrace
5.1 Hierarchy Viewer

简介
Android Studio 提供的UI性能检测工具。

作用
可视化获得UI布局设计结构 & 各种属性信息,帮助我们优化布局设计
即 :方便查看Activity布局,各个View的属性、布局测量-布局-绘制的时间
具体使用
Hierarchy Viewer 使用指南
5.2 Lint

简介
Android Studio 提供的 代码扫描分析工具
作用
扫描、发现代码结构 / 质量问题;提供解决方案
该过程不需手写测试用例
Lint 发现的每个问题都有描述信息 & 等级(和测试发现 bug 很相似),可方便定位问题 & 按照严重程度进行解决
具体使用
Lint 使用指南

5.3 Systrace

简介
Android 4.1以上版本提供的性能数据采样 & 分析工具
作用
检测 Android系统各个组件随着时间的运行状态 & 提供解决方案

收集 等运行信息,从而帮助开发者更直观地分析系统瓶颈,改进性能
检测范围包括:Android 关键子系统(如WindowManagerService 等 Framework 部分关键模块)、服务、View系统
功能包括:跟踪系统的I/O 操作、内核工作队列、CPU 负载等,在 UI 显示性能分析上提供很好的数据,特别是在动画播放不流畅、渲染卡等问题上
具体使用
Systrace 使用指南

6. 总结

本文主要讲解Android 性能优化中的 布局优化
示意图

下面我将继续深入讲解 Android中的性能优化 ,有兴趣可以继续关注Carson_Ho的安卓开发笔记
请帮顶 / 评论点赞!因为你的鼓励是我写作的最大动力!

版权声明:本文为博主原创文章,未经博主允许不得转载,更多文章请继续关注Carson_Ho的博客! http://blog.csdn.net/carson_ho/article/details/79620486

在 Android开发中,性能优化策略十分重要的更多相关文章

  1. android开发中图片优化步骤

    android开发中图片优化方法 1.图片加载方法,方便用户加载图片 /*** * 加载本地图片 * @param context:主运行函数实例 * @param bitAdress:图片地址,一般 ...

  2. Android开发——布局性能优化的一些技巧(一)

    0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...

  3. Cocos开发中性能优化工具介绍之Visual Studio内存泄漏检测工具——Visual Leak Detector

    那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测功能,我们可以使用第三方工具Visual Leak Detector(以下简 ...

  4. Cocos开发中性能优化工具介绍之使用Windows任务管理器

    说到Windows平台,我们很快就想到了Visual Studio 2012,然而Visual Studio 2012在这方面没有很好的工具.如果我们只是想知道大体上内存.CPU等在某一事件前后变化情 ...

  5. Cocos开发中性能优化工具介绍之Xcode中Instruments工具使用

    Instruments是动态分析工具,它与Xcode集成在一起,可以在Xcode中通过菜单Product→Profile启动.启动如图所示,Instruments有很多跟踪模板可以动态分析和跟踪内存. ...

  6. Android开发——布局性能优化的一些技巧(二)

    , 0, drawable.getMinimumWidth(),dra.getMinimumHeight()); tv.setCompoundDrawables(null, null, drawabl ...

  7. Android开发中常用的ListView列表的优化方式ViewHolder

    在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...

  8. 在android开发中使用multdex的方法-IT蓝豹为你整理

    Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...

  9. 如何在Android开发中让你的代码更有效率

    最近看了Google IO 2012年的一个视频,名字叫做Doing More With Less: Being a Good Android Citizen,主要是讲如何用少少的几句代码来改善And ...

随机推荐

  1. Yii 2.0 使用片段缓存

    网站首页footer中的菜单标题是从数据库读取并显示处理的. 也就是 <footer>标题里面是foreach.这样每个人打开网站就查询遍历效率会很低. <footer class= ...

  2. MapReduce和yarn

    1.Mapreduce是什么? Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带默 ...

  3. PHPCMS如何让手机站点取消浏览大图直接加载原图

    一.然后找到phpcms\modules\wap\functions\global.func.php 文件,找到相关代码,如下图: return '<img src="'.thumb( ...

  4. Github上的一些高分Qt开源项目【多图】

    游戏2D地图编辑器: 著名的TileMap编辑器,做2D游戏开发的一定不会陌生. Go 语言的IDE: Go语言的集成开发环境. Clementine Music Player: 功能很完善且跨平台支 ...

  5. 亚马逊6月18日发布惊世之作 或为3D智能手机

    亚马逊将在 6 月 18 日举行一个产品发布会. 其内容可能是关于传闻已久的亚马逊智能手机.该公司在 YouTube 上公布了一段炫耀这款设备的视频.这段视频展示了很多人在这款产品前摇头晃脑,并且表现 ...

  6. Python3 函数作用域

    一 LEGB 什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 顺序是什么? 跟名字一样 ...

  7. Live Archive 训练题

    7091 Height Ordering Mrs. Chambers always has her class line up in height order (shortest at the fro ...

  8. USACO 2.3.4 Money Systems 货币系统(完全背包)

    Description 母牛们不但创建了他们自己的政府而且选择了建立了自己的货币系统. [In their own rebellious way],,他们对货币的数值感到好奇. 传统地,一个货币系统是 ...

  9. 每日Scrum--No.2

    Yesterday:找地图 Today: 找到最适合我们软件的地图版本 Problem:找不到特别匹配的版本

  10. 用到的C++标准库

    std::set<type>, 模板写的平衡二叉树的集合容器, method: insert, count, std:map<int, string>, 映射和多重映射基于某一 ...