Android SDK工具:使用layoutopt进行布局优化
2011-05-28 09:46
483 查看
创建好看的Android布局是个不小的挑战,当你花了数小时调整好它们适应多种设备后,你通常不想再重新调整,但笨重的嵌套布局效率往往非常低下,幸运的是,在Android SDK中有一个工具可以帮助你优化布局,以减少内存消耗,提高应用程序运行性能。
layoutoptimization
优化是需要一定技巧的,性能良好的代码固然重要,但写出优秀代码的成本往往也很高,你可能不会过早地贸然为那些只运行一次或临时功能代码实施优化,
如果你的应用程序反应迟钝,并且卖得很贵,或使系统中的其它应用程序变慢,用户一定会有所响应,你的应用程序下载量将很可能受到影响。
在开发期间尽早优化你的布局是节省成本,提高性能的简单方法,Android SDK带来了一个工具,它可以自动分析你的布局,发现可能并不需要的布局元素,以降低布局复杂度。
第一步:准备工作
如果想使用Android SDK中提供的优化工具,你需要在开发系统的命令行中工作,如果你不熟悉使用命令行工具,那么你得多下功夫学习了。
我们强烈建议你将Android工具所在的路径添加到操作系统的环境变量中,这样就可以直接敲名字运行相关的工具了,否则每次都要在命令提示符后面
输入完整的文件路径,现在在Android
SDK中有两个工具目录:/tools和/platform-tools,本文主要使用位于/tools目录中的layoutopt工具,另外我想说的
是,ADB工具位于/platform-tools目录下。
运行layoutopt
运行layoutopt工具是相当简单的,只需要跟上一个布局文件或布局文件所在目录作为参数,需要注意的是,这里你必须包括布局文件或目录的完整路径,即使你当前就位于这个目录。我们来看一个简单的例子:
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给出的优化建议,虽然采纳建议
作出修改不会立即大幅改善性能,但没有理由需要复杂的布局拖慢整个应用程序的速度,并且后期的维护难度也很大。简单布局不仅简化了开发周期,还可以减少测
试和维护工作量,因此,在应用程序开发期间,应尽早优化你的布局,不要等到最后用户反馈回来再做修改。
原文名:Android SDK Tools: Layout Optimization 作者:Shane Conder和Lauren Darcey
原文
layoutoptimization
优化是需要一定技巧的,性能良好的代码固然重要,但写出优秀代码的成本往往也很高,你可能不会过早地贸然为那些只运行一次或临时功能代码实施优化,
如果你的应用程序反应迟钝,并且卖得很贵,或使系统中的其它应用程序变慢,用户一定会有所响应,你的应用程序下载量将很可能受到影响。
在开发期间尽早优化你的布局是节省成本,提高性能的简单方法,Android SDK带来了一个工具,它可以自动分析你的布局,发现可能并不需要的布局元素,以降低布局复杂度。
第一步:准备工作
如果想使用Android SDK中提供的优化工具,你需要在开发系统的命令行中工作,如果你不熟悉使用命令行工具,那么你得多下功夫学习了。
我们强烈建议你将Android工具所在的路径添加到操作系统的环境变量中,这样就可以直接敲名字运行相关的工具了,否则每次都要在命令提示符后面
输入完整的文件路径,现在在Android
SDK中有两个工具目录:/tools和/platform-tools,本文主要使用位于/tools目录中的layoutopt工具,另外我想说的
是,ADB工具位于/platform-tools目录下。
运行layoutopt
运行layoutopt工具是相当简单的,只需要跟上一个布局文件或布局文件所在目录作为参数,需要注意的是,这里你必须包括布局文件或目录的完整路径,即使你当前就位于这个目录。我们来看一个简单的例子:
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给出的优化建议,虽然采纳建议
作出修改不会立即大幅改善性能,但没有理由需要复杂的布局拖慢整个应用程序的速度,并且后期的维护难度也很大。简单布局不仅简化了开发周期,还可以减少测
试和维护工作量,因此,在应用程序开发期间,应尽早优化你的布局,不要等到最后用户反馈回来再做修改。
原文名:Android SDK Tools: Layout Optimization 作者:Shane Conder和Lauren Darcey
原文
相关文章推荐
- Android SDK工具:使用layoutopt进行布局优化
- Android SDK工具:使用layoutopt进行布局优化
- Android SDK工具:使用layoutopt进行布局优化
- Android SDK工具:使用layoutopt进行布局优化
- Android SDK工具:使用layoutopt进行布局优化
- Android SDK工具:使用layoutopt(lint)进行布局优化
- 教你使用Android SDK布局优化工具layoutopt
- 使用layoutopt【lint.bat】进行布局优化
- 教你使用Android SDK布局优化工具layoutopt
- 【layoutopt】使用layoutopt进行布局优化
- 使用 layoutopt 进行布局优化
- 使用LayoutOpt进行布局优化
- UI优化技巧:使用layoutopt进行布局优化
- Android之优化技术 --- 使用layoutopt进行布局优化
- 教你使用Android SDK布局优化工具layoutopt
- 布局优化必备 Hierarchy Viewer 工具使用
- [Android]使用 lint 工具优化 Android 布局
- 使用android SDK自带 Lint工具优化源码
- Android 性能优化 一 布局优化工具Hierarchy Viewer的使用
- 使用Facebook的Redex工具进行android app运行优化