zoukankan      html  css  js  c++  java
  • Android布局的优化方案汇总

    创建好看的Android布局是个不小的挑战,当你花了数小时调整好它们适应多种设备后,你通常不想再重新调整,但笨重的嵌套布局效率往往非常低下,幸运的是,在Android SDK中有一个工具可以帮助你优化布局,以减少内存消耗,提高应用程序运行性能。

    layout_optimization 
    layoutoptimization

    优化是需要一定技巧的,性能良好的代码固然重要,但写出优秀代码的成本往往也很高,你可能不会过早地贸然为那些只运行一次或临时功能代码实施优化,如果你的应用程序反应迟钝,并且卖得很贵,或使系统中的其它应用程序变慢,用户一定会有所响应,你的应用程序下载量将很可能受到影响。

    在开发期间尽早优化你的布局是节省成本,提高性能的简单方法,Android SDK带来了一个工具,它可以自动分析你的布局,发现可能并不需要的布局元素,以降低布局复杂度。

    第一步:准备工作

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

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

    运行layoutopt

    运行layoutopt工具是相当简单的,只需要跟上一个布局文件或布局文件所在目录作为参数,需要注意的是,这里你必须包括布局文件或目录的完整路径,即使你当前就位于这个目录。我们来看一个简单的例子:
     

    1. D:d oolseclipsearticle_wsNothing eslayout>layoutopt D:d oolseclipsearticle_wsNothing eslayoutmain.xml  
    2. D:d oolseclipsearticle_wsNothing eslayoutmain.xml  
    3. D:d oolseclipsearticle_wsNothing eslayout> 

    注意,在上面的示例中,包含了文件的完整路径,如果不指定完整路径,不会输出任何内容,例如:
     

    1. D:d oolseclipsearticle_wsNothing eslayout>layoutopt main.xml  
    2. D:d oolseclipsearticle_wsNothing eslayout> 

    因此,如果你看不任何东西,则很可能是文件未被解析,也就是说文件可能未被找到。

    使用layoutopt输出

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

    无用的布局

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

    1. <?xml version="1.0" encoding="utf-8"?> 
    2. <LinearLayout 
    3.     xmlns:android="http://schemas.android.com/apk/res/android" 
    4.     android:layout_width="match_parent" 
    5.     android:layout_height="match_parent" 
    6.     android:orientation="horizontal"> 
    7.     <LinearLayout 
    8.         android:id="@+id/linearLayout1" 
    9.         android:layout_height="wrap_content" 
    10.         android:layout_width="wrap_content" 
    11.         android:orientation="vertical"> 
    12.         <TextView 
    13.             android:id="@+id/textView1" 
    14.             android:layout_width="wrap_content" 
    15.             android:text="TextView" 
    16.             android:layout_height="wrap_content"></TextView> 
    17.     </LinearLayout> 
    18. </LinearLayout> 

    工具将会很快告诉我们LinearLayout内的LinearLayout是多余的:
     

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

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

    根可以替换

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

    1. <?xml version="1.0" encoding="utf-8"?> 
    2. <FrameLayout 
    3.     xmlns:android="http://schemas.android.com/apk/res/android" 
    4.     android:layout_width="match_parent" 
    5.     android:layout_height="match_parent"> 
    6.     <LinearLayout 
    7.         android:id="@+id/linearLayout1" 
    8.         android:layout_height="wrap_content" 
    9.         android:layout_width="wrap_content" 
    10.         android:orientation="vertical"> 
    11.         <TextView 
    12.             android:id="@+id/textView1" 
    13.             android:layout_width="wrap_content" 
    14.             android:text="TextView" 
    15.             android:layout_height="wrap_content"></TextView> 
    16.         <TextView 
    17.             android:text="TextView" 
    18.             android:id="@+id/textView2" 
    19.             android:layout_width="wrap_content" 
    20.             android:layout_height="wrap_content"></TextView> 
    21.     </LinearLayout> 
    22. </FrameLayout> 

    这个布局将返回下面的输出:
     

    1. 5:22 The root-level <FrameLayout/> can be replaced with <merge/> 
    2. 10:21 This LinearLayout layout or its FrameLayout parent is useless  

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

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

    太多的视图

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

    1. -1:-1 This layout has too many views: 83 views, it should have <= 80!  
    2. -1:-1 This layout has too many views: 82 views, it should have <= 80!  
    3. -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:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!  
    2. 305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless  
    3. 307:314 This LinearLayout layout or its FrameLayout parent is possibly useless  
    4. 310:312 This LinearLayout layout or its LinearLayout parent is possibly useless  

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

    小结

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


  • 相关阅读:
    Java程序设计11——异常处理
    Hive安装及配置
    zookeeper集群安装(转)
    Linux网络服务管理命令
    Linux网络配置常用命令
    IP地址及子网掩码计算
    linux ftp、sftp、telnet服务开通、更改Orale最大连接数
    SQL多表连接
    用css伪类实现提示框效果
    关于DOM操作的性能优化
  • 原文地址:https://www.cnblogs.com/xieping/p/4784539.html
Copyright © 2011-2022 走看看