Optimizing Layout Hierarchies

This lesson teaches you to

  1. Inspect Your Layout
  2. Revise Your Layout
  3. Use Lint

You should also read

  It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing. For example, using nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice. This is particularly important when the layout is inflated repeatedly, such as when used in a ListView or GridView.

 嵌套使用基本的layout会产生一个很深的layout继承结构。比如嵌套的linearlayout,由其在其中使用layout_weight时,会产生两次measure。

  In this lesson you'll learn to use Hierarchy Viewer and Layoutopt to examine and optimize your layout.

Inspect Your Layout

  The Android SDK tools include a tool called Hierarchy Viewer that allows you to analyze your layout while your application is running. Using this tool helps you discover bottlenecks in the layout performance.

 Hierarchy Viewer 可以发现有性能瓶颈的view或layout。

  Hierarchy Viewer works by allowing you to select running processes on a connected device or emulator, then display the layout tree. The traffic lights on each block represent its Measure, Layout and Draw performance, helping you identify potential issues.

  For example, figure 1 shows a layout that's used as an item in a ListView. This layout shows a small bitmap image on the left and two stacked items of text on the right. It is especially important that layouts that will be inflated multiple times—such as this one—are optimized as the performance benefits will be multiplied.

      

      Figure 1. Conceptual layout for an item in a ListView.

  The hierarchyviewer tool is available in <sdk>/tools/. When opened, the Hierarchy Viewer shows a list of available devices and its running components. Click Load View Hierarchy to view the layout hierarchy of the selected component. For example, figure 2 shows the layout for the list item illustrated by figure 1.

      

      Figure 2. Layout hierarchy for the layout in figure 1, using nested instances of LinearLayout.

              

      Figure 3. Clicking a hierarchy node shows its performance times.

  In figure 2, you can see there is a 3-level hierarchy with some problems laying out the text items. Clicking on the items shows the time taken for each stage of the process (figure 3). It becomes clear which items are taking the longest to measure, layout, and render, and where you should spend time optimizing.

  The timings for rendering a complete list item using this layout are:

  • Measure: 0.977ms
  • Layout: 0.167ms
  • Draw: 2.717ms

Revise Your Layout 改善上例中的layout

  Because the layout performance above slows down due to a nested LinearLayout, the performance might improve by flattening the layout—make the layout shallow and wide, rather than narrow and deep. A RelativeLayout as the root node allows for such layouts. So, when this design is converted to use RelativeLayout, you can see that the layout becomes a 2-level hierarchy. Inspection of the new layout looks like this:

 上例中使用了多层嵌套的linearlayout,其实没有必要,可以用一个RelativeLayout对其优化。
 在实战中尽量让布局层次扁平,不要有过深的层次。

      

      Figure 4. Layout hierarchy for the layout in figure 1, using RelativeLayout.

  

 改善后的时间:

  Now rendering a list item takes:

  • Measure: 0.598ms
  • Layout: 0.110ms
  • Draw: 2.146ms

  Might seem like a small improvement, but this time is multiplied several times because this layout is used for every item in a list.

  Most of this time difference is due to the use of layout_weight in the LinearLayout design, which can slow down the speed of measurement. It is just one example of how each layout has appropriate uses and you should carefully consider whether using layout weight is necessary.

 除非 layout_weight 必要,否则少用它,它会产生重复的measure,导致性能变差。 

Use Lint

  It is always good practice to run the lint tool on your layout files to search for possible view hierarchy optimizations. Lint has replaced the Layoutopt tool and has much greater functionality. Some examples of lint rules are:

 可以用lint对布局xml进行扫描,会发现有性能问题的部分,下面是lint扫描时遵守的几条规则:
  • Use compound drawables - A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable.

    如果一个linearlayout中只有一个ImageView和TextView,可以用TextView的compound 简化。
  • Merge root frame - If a FrameLayout is the root of a layout and does not provide background or padding etc, it can be replaced with a merge tag which is slightly more efficient.
    布局文件的根节点是<FrameLayout>时,并且没在其内定义背景,间距等,可以用<merge>代替它。
  • Useless leaf - A layout that has no children or no background can often be removed (since it is invisible) for a flatter and more efficient layout hierarchy.
    没有子布局并没有设置背景的可以删除掉。
  • Useless parent - A layout with children that has no siblings, is not a ScrollView or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.
    一个布局没有兄弟,不是ScrollView,不是根layout,没有背景,就可以直接把它的孩子移到上层布局上,然后删除它。
  • Deep layouts - Layouts with too much nesting are bad for performance. Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance. The default maximum depth is 10.
    嵌套过深的布局会产生性能问题,用RelativeLayout或GridLayout替代它。默认最大深度为10。

  Another benefit of Lint is that it is integrated into Android Studio. Lint automatically runs whenever you compile your program. With Android Studio, you can also run lint inspections for a specific build variant, or for all build variants.

  You can also manage inspection profiles and configure inspections within Android Studio with the File>Settings>Project Settings option. The Inspection Configuration page appears with the supported inspections.

          Figure 5. Inspection Configuration

  Lint has the ability to automatically fix some issues, provide suggestions for others and jump directly to the offending code for review.

Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局的更多相关文章

  1. Android性能优化之布局优化

    最新最准确内容建议直接访问原文:Android性能优化之布局优化 本文为Android性能优化的第二篇——布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不 ...

  2. 【转】Android性能优化之布局优化篇

     转自:http://blog.csdn.net/feiduclear_up/article/details/46670433 Android性能优化之布局优化篇 分类: andorid 开发2015 ...

  3. [Android 性能优化系列]降低你的界面布局层次结构的一部分

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...

  4. Android性能优化之UI渲染性能优化

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要记录一些工作中常用的UI渲染性能优化及调试方法,理解这些方法对于我们编写高质量代码也是有一些帮助的,主要内容包括介绍CPU,GPU的职 ...

  5. Android中,如何提升Layout的性能?

    Layout 是 Android 应用中直接影响用户体验的关键部分.如果实现的不好,你的 Layout 会导致程序非常占用内存并且 UI 运行缓慢.Android SDK 带有帮助你找到 Layout ...

  6. Android性能优化之中的一个 布局优化

    本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...

  7. [转] Android 性能分析案例

    Android 系统的一个工程师(Romain Guy)针对Falcon Pro  应用,撰写了一个Android性能分析的文章.该文章介绍了如何分析一个应用哪里出现了性能瓶颈,导致该应用使用起来不流 ...

  8. Android 性能优化(2)性能工具之「Hierarchy Viewer 」Optimizing Your UI:分析哪个view有性能问题,查看屏幕上某像素点的坐标,颜色等

    Optimizing Your UI In this document Using Hierarchy Viewer Running Hierarchy Viewer and choosing a w ...

  9. Android性能优化之渲染

    Google近期在Udacity上发布了Android性能优化的在线课程,目前有三个篇章,分别从渲染,运算与内存,电量三个方面介绍了如何去优化性能,这些课程是Google之前在Youtube上发布的A ...

随机推荐

  1. 【PowerDesigner】PowerDesigner之CDM、PDM、SQL之间转换

    有关CDM.PDM.SQL之间转换以及不同数据库之间库表Sql的移植,首先要了解的是它们各自的用途.这里就简单的描述一下,不做详细的解释了. CDM:概念数据模型.CDM就是以其自身方式来描述E-R图 ...

  2. Linux下汇编语言学习笔记20 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  3. Ubuntu 12.04 之 LAMP

    搭建LAMP环境 (1)更新软件列表: sudo apt-get update 结果报错: W: 无法下载 bzip2:/var/lib/apt/lists/partial/cn.archive.ub ...

  4. maven提示“编码 GBK 的不可映射字符”问题的解决

    pom.xml中加上如下代码 <properties> <!-- spring版本号 --> <spring.version>4.2.3.RELEASE</s ...

  5. Linux学习系列之LNMP

    LNMP介绍 LNMP是什么 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web架构; 这四种软件组合,可以成为一个免费.高效.扩展性强的Web架构; LNMP原理图 ...

  6. web开发常见性能优化方式

    经常使用的高并发. 高性能web,数据库server.  1.html 静态化 : 如新闻频道更新的非常快,都是通过cms静态生成(门户,信息公布类型的站点,交互性高的如猫扑的大杂烩也是静态化,实时静 ...

  7. Kafka跨集群同步工具——MirrorMaker

    MirrorMaker是为解决Kafka跨集群同步.创建镜像集群而存在的.下图展示了其工作原理.该工具消费源集群消息然后将数据又一次推送到目标集群. watermark/2/text/aHR0cDov ...

  8. js中的封装、继承、多态

    Javascript是一门解释型的语言,是基于对象的,并不是真正的面向对象的语言,对变量类型的应用也是宽松的,其实它同样可以模拟面向对象的功能:  1 function myfun1(){  2    ...

  9. 2013级C++第12周(春)项目——成员的訪问属性、多重继承

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读 1.阅读程序.分析类中成员 ...

  10. JFreeChart简单用法

    需要用到的包 jfreechart-0.9.20.jar,jcommon-0.9.5.jar 创建一般步骤: 1.生成org.jfree.data.DefaultCategoryDataset对象,方 ...