Layout Optimization布局优化工具

来源:互联网 发布:抢票软件收费 编辑:程序博客网 时间:2024/06/08 06:46


Layout Optimization工具

如果想使用Android SDK中提供的优化工具,你需要在开发系统的命令行中工作,如果你不熟悉使用命令行工具,那么你得多下功夫学习了。

我们强烈建议你将Android工具所在的路径添加到操作系统的环境变量中,这样就可以直接敲名字运行相关的工具了,否则每次都要在命令提示符后面输入完整的文件路径,现在在Android SDK中有两个工具目录:/tools和/platform-tools,本文主要使用位于/tools目录中的layoutopt工具。


layoutopt工具

运行layoutopt工具是相当简单的,只需要以上一个布局文件或布局文件所在目录作为参数:

layoutopt <list of xml files or directories>

需要注意的是,这里你必须包括布局文件或目录的完整路径,即使你当前就位于这个目录。

我们来看一个简单的例子:

D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml  D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml  D:\d\tools\eclipse\article_ws\Nothing\res\layout> 
注意,在上面的示例中,包含了文件的完整路径,如果不指定完整路径,不会输出任何内容,例如:
D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml  D:\d\tools\eclipse\article_ws\Nothing\res\layout> 
因此,如果你看不任何东西,则很可能是文件未被解析,也就是说文件可能未被找到。
当包含多个路径的时候,用空格间隔。

使用layoutopt输出

Layoutopt的输出结果只是建议,你可以有选择地在你的应用程序中采纳这些建议,下面来看几个使用layoutopt输出建议的例子。


无用的布局:

在布局设计期间,我们会频繁地移动各种组件,有些组件最终可能会不再使用,如:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal">     <LinearLayout         android:id="@+id/linearLayout1"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:orientation="vertical">         <TextView             android:id="@+id/textView1"             android:layout_width="wrap_content"             android:text="TextView"             android:layout_height="wrap_content"></TextView>     </LinearLayout> </LinearLayout> 
工具将会很快告诉我们LinearLayout内的LinearLayout是多余的:

11:17 This LinearLayout layout or its LinearLayout parent is useless 

输出结果每一行最前面的两个数字表示建议的行号。


根可以替换:

Layoutopt的输出有时是矛盾的,例如:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">     <LinearLayout         android:id="@+id/linearLayout1"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:orientation="vertical">         <TextView             android:id="@+id/textView1"             android:layout_width="wrap_content"             android:text="TextView"             android:layout_height="wrap_content"></TextView>         <TextView             android:text="TextView"             android:id="@+id/textView2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"></TextView>     </LinearLayout> </FrameLayout> 
这个布局将返回下面的输出:
5:22 The root-level <FrameLayout/> can be replaced with <merge/> 10:21 This LinearLayout layout or its FrameLayout parent is useless  

第一行的建议虽然可行,但不是必需的,我们希望两个TextView垂直放置,因此LinearLayout应该保留,而第二行的建议则可以采纳,可以删除无用的FrameLayout。

有趣的是,这个工具不是全能的,例如,在上面的例子中,如果我们给FrameLayout添加一个背景属性,然后再运行这个工具,第一个建议当然会消失,但第二个建议仍然会显示,工具知道我们不能通过合并控制背景,但检查了LinearLayout后,它似乎就忘了我们还给FrameLayout添加了一个LinearLayout不能提供的属性。


太多的视图:

每个视图都会消耗内存,在一个布局中布置太多的视图,布局会占用过多的内存,假设一个布局包含超过80个视图,layoutopt可能会给出下面这样的建议:

-1:-1 This layout has too many views: 83 views, it should have <= 80!  -1:-1 This layout has too many views: 82 views, it should have <= 80!  -1:-1 This layout has too many views: 81 views, it should have <= 80!

上面给出的建议是视图数量不能超过80,当然最新的设备有可能能够支持这么多视图,但如果真的出现性能不佳的情况,最好采纳这个建议。


嵌套太多:

布局不应该有太多的嵌套,layoutopt(和Android团队)建议布局保持在10级以内,即使是最大的平板电脑屏幕,布局也不应该超过10级,RelativeLayout可能是一个解决办法,但它的用法更复杂,好在Eclipse中的Graphical Layout资源工具更新后,使得这一切变得更简单。

下面是布局嵌套太多时,layoutopt的输出内容:

-1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!  305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless  307:314 This LinearLayout layout or its FrameLayout parent is possibly useless  310:312 This LinearLayout layout or its LinearLayout parent is possibly useless 

嵌套布局警告通常伴随有一些无用布局的警告,有助于找出哪些布局可以移除,避免屏幕布局全部重新设计。


小结:

Layoutopt是一个快速易用的布局分析工具,找出低效和无用的布局,你要做的是判断是否采纳layoutopt给出的优化建议,虽然采纳建议作出修改不会立即大幅改善性能,但没有理由需要复杂的布局拖慢整个应用程序的速度,并且后期的维护难度也很大。简单布局不仅简化了开发周期,还可以减少测试和维护工作量,因此,在应用程序开发期间,应尽早优化你的布局,不要等到最后用户反馈回来再做修改。







0 0
原创粉丝点击