`
izuoyan
  • 浏览: 8933644 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【Android Advanced Training - 02】提高显示布局文件的性能[Lesson 1 - 优化布局层级]

 
阅读更多


Optimizing Layout Hierarchies [优化Layout层级]

  • Layout是Android程序影响用户体验最关键的一部分。如果布局文件不好会使得程序比较卡。SDK里面包含了一些工具用来帮助我们发现布局文件的性能问题
  • 这里有一个共同的误区:使用基本的Layout结构是最有效的。但是,每一个添加到系统的组件都需要初始化,进行布局,绘制的过程。比如,使用在LinearLayout里面使用子组件会导致一个过于deep的层级结构。而且内嵌使用包含layout_weight属性的LinearLayout会在绘制时花费昂贵的系统资源,因为每一个子组件都需要被测量两次。在使用ListView与GridView的时候,这个问题显的尤其重要,因为子组件会重复被创建


这一课我们会学习使用Hierarchy Viewer andLint 来检查并最优化布局文件。

Inspect Your Layout[查看Layout]

Android SDK里面包含了一个叫做Hierarchy Viewer的工具,在程序运行的时候分析布局文件,从而找住性能瓶颈

连接上设备,打开Hierarchy Viewer(定位到tools/目录下,直接执行hierarchyviewer的命令,选定需要查看的Process,再点击Load View Hierarchy会显示出当前界面的布局Tree。在每个模块的Traffic light上有三个灯,分别代表了Measure, Layout and Draw三个步骤的性能。


Figure 1.ListView每个Item的常见布局.


Figure 2.上面显示了对应与图片1的布局层级信息.可以看到中间LinearLayout的Measure的灯是红色的,这就是因为上面说到的:使用内嵌layout_weight的属性的LinearLayout会导致测量时花费了双倍的时间。


Figure 3.点击某个模块会显示具体每个步骤所花费的时间。

Revise Your Layout [修改你的Layout]


【使得Layout宽而浅,而不是窄而深(在Hierarchy Viewer的Tree视图里面体现)】

上面的布局我们可以使用RelativeLayout来替代LinearLayout,从而实现shallow and wide.


Figure 4.改用RelativeLayout来实现图片1的布局。

可以看到这是一个小的优化,可是这带来的效果是明显的,因为在ListView里面会出现很多这样的布局。

导致前面的case会出现花费时间比较多的愿意是使用了layout_weight在LinearLayout。我们需要仔细评估到底是否需要使用那样的

布局,尽量避免使用layout_weight。

Use Lint [使用Lint]

Lint是一款在ADT 16才出现用来替代layoutopt的新型工具,具有更强大的功能。


Lint的使用规则如下:

  • Usecompounddrawables - ALinearLayoutwhich contains anImageViewand aTextViewcan be moreefficientlyhandled as a compounddrawable.
  • 【使用compound drawables - 一个包含了ImageView与TextView的LinearLayout可以被当作一个compound drawable来处理】
  • Mergerootframe- If aFrameLayoutis 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.
  • 【使用merge根框架 - 如果FramLayout仅仅是一个纯粹的(没有设置背景,间距等)布局根元素,我们可以使用merge标签来当作根标签】
  • Useless leaf - A layout that has no children or no background can often be removed (since it isinvisible) for a flatter and more efficient layout hierarchy.
  • 【无用的分支 - 如果一个layout并没有任何子组件,那么可以被移除,这样可以提高效率】
  • Useless parent - A layout with children that has nosiblings, is not aScrollViewor a rootlayout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and moreefficientlayouthierarchy.
  • 【无用的父控件 - 如果一个layout只有子控件,没有兄弟控件,并且不是一个ScrollView或者根节点,而且没有设置背景,那么我们可以移除这个父控件,直接把子控件提升为父控件】
  • Deep layouts - Layouts with too much nesting are bad for performance. Consider using flatter layouts such asRelativeLayoutorGridLayoutto improve performance. The default maximum depth is 10.
  • 【深层次的layout - 尽量减少内嵌的层级,考虑使用更多平级的组件RelativeLayoutorGridLayout来提升布局性能,默认最大的深度是10


Eclipse会自动运行Lint的工具,并给出相应的提醒,不管是在导出APK,编辑,保存XML还是在使用layout编辑器的时候。如果想

强制运行,请参看上面的图标,点击运行

如果没有安装ADT 16,需要在命令行中执行。


学习自:http://developer.android.com/training/improving-layouts/optimizing-layout.html

转载请注明出处:http://blog.csdn.net/kesenhoo,谢谢!











分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics