当创建复杂布局的时候,我们会在xml 文件中添加大量的ViewGroup和View。伴随着每次迭代,View树的层次越来越深,界面加载速度越来越慢,消耗的内存也越来越多。当您的程序出现加载时短暂黑屏或横竖切换时短暂黑屏,抑或如内存溢出(OOM)之类的问题时,没准您的程序需要优化了。

那么如何让程序运行速度更快?响应更敏捷?优化布局是一个最基本的方法,本文将介绍最基本的优化布局方法。

1.使用ViewStub实现View的延迟加载。

很多情况下,xml布局文件中的部分View初始状态是设置为不显示的(android:visible="invisible"或"gone")的。如您的代码中存在这种设置的View,您可以尝试使用ViewStub.

Android开发文档中对ViewStub的介绍如下:

http://developer.android.com/reference/android/view/ViewStub.html

“ViewStub”是一种不可见并且大小为0的试图,可以延迟到运行时加载(Inflate)布局资源。当ViewStub被设置为可见或者调用inflate()方法时,才会加载布局资源。然后ViewStub便被加载的布局替代。

现在我们使用Eclipse创建一个新工程,默认Fragment的布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testlayout.MainActivity$PlaceholderFragment" >

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Inflate View Stub" />

<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/button" >

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </LinearLayout>

</RelativeLayout>

如上可见,LinearLayout中嵌套了多个子View。如果在Activity载入的时候不需要立即显示这部分内容,我们就可以使用ViewStub将这LinearLayout替换掉。

修改后的布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testlayout.MainActivity$PlaceholderFragment" >
    <Button    <!--  点击该Button后加载ViewStub -->
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Inflate View Stub" />

    <ViewStub
        android:id="@+id/viewStub"
        android:inflatedId="@+id/view_stub_layout"      <!-- 将会替换掉子布局中的最外层ViewGroup的id -->
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"
        android:layout="@layout/view_stub_layout"/>   <!-- 加载的子布局 -->
</RelativeLayout>

其中省去的LinearLayout放入新的布局文件view_stub_layout.xml中,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

最后完成我们的Java文件,当点击Button时,执行以下代码:

        private void showViewStub() {
            ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub);
            if (stub != null) {
                Log.i("xzy", "stub is not null");
//                stub.setLayoutResource(R.layout.view_stub_layout);   //如果布局文件中未设置android:layout="@layout/view_stub_layout",可以这样设置子布局
                stub.inflate();
                View view = (View)  mRootView.findViewById(R.id.linearLayout);
                if (view != null) {
                    view.setBackgroundColor(Color.RED);
                }
            } else {
                Log.i("xzy", "stub is Null");
            }
        }

ViewStub加载完之后将变为Null,此后就无法通过ViewStub对象加载子布局了,所以不会重复加载。

Android App性能优化(一)之布局优化的更多相关文章

  1. 十大技巧优化Android App性能

    无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的. 那么,如何能开发出更高性能的Android App?相信是软 ...

  2. 优化Android App性能?十大技巧必知!

    无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的.那么,如何能开发出更高性能的Android App?相信是软件 ...

  3. Android APP 性能优化的一些思考

    说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...

  4. 包建强的培训课程(9):Android App性能优化

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  5. Android app 性能优化的思考--性能卡顿不好的原因在哪?

    说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...

  6. android app性能优化大汇总

    这里根据网络上各位大神已经总结的知识内容做一个大汇总,作为记录,方便后续“温故知新”. 性能指标: (1)使用流畅度:  图片处理器每秒刷新的帧数(FPS),可用来指示页面是否平滑的渲染.高的帧率可以 ...

  7. Android APP性能分析方法及工具

    近期读到<Speed up your app>一文.这是一篇关于Android APP性能分析.优化的文章.在这篇文章中,作者介绍他的APP分析优化规则.使用的工具和方法.我觉得值得大家借 ...

  8. Android App性能测试之adb命令

    本篇文章总结了Android App性能测试过程中常用的adb命令.通过这些adb命令,可以查看App的性能数据,为评判性能好坏作参考. CPU相关 显示占用CPU最大的5个应用 adb shell ...

  9. Android App 性能优化实践

    本文记录了Android App优化需要用到的工具和以及在实践中的Tips.也算对我这半年来部分工作的总结. 工具 Hierarchy Viewer 是 Android SDK 自带的 Layout ...

  10. android app性能优化大汇总(内存性能优化)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上 ...

随机推荐

  1. InnoDB索引概述,二分查找法,平衡二叉树

    索引是应用程序设计和开发的一个重要方面.如果索引太多,应用的性能可能会受到影响:如果索引太少,对查询性能又会产生影响.要找到一个合适的平衡点,这对应用的性能至关重要. 如果知道数据的使用,从一开始就应 ...

  2. WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)

    这里使用了一个进度条来展示, 前段代码: <Window x:Class="TimerTest.MainWindow" xmlns="http://schemas. ...

  3. ant使用小结

    使用builder.xml的方式:完成的工作:打jar包并运行,其中引用了第三方jar和配置文件: <?xml version="1.0" encoding="UT ...

  4. [转]NancyFx/Nancy

    本文转自:https://github.com/NancyFx/Nancy/wiki/Documentation Getting Started Introduction Exploring the ...

  5. synchronized同步锁

    在多线程的情况下,由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Java语言提供了专门机制以解决这种冲突,有效避免了同一个数据对象被多个线程同时访问.由于 ...

  6. WCF 之部署(VS2010)

    一. 环境vs2010,WCF应用程序,server 2008 第一步:WCF项目右键点击项目,选择生成部署包,如下图: 第二步:WCF项目上右键,选择:在windows资源管理器中打开文件夹,如下图 ...

  7. [PHP] 通用网关接口CGI 的运行原理

    CGI 的运行原理:1.客户端访问某个 URL 地址之后,通过 GET/POST/PUT 等方式提交数据,并通过 HTTP 协议向 Web 服务器发出请求.2.服务器端的 HTTP Daemon(守护 ...

  8. 十二、异步工具Timer

    一.简介 JDK提供一种异步线程工具Timer类,你可以利用这个类做延迟任务.周期性任务等. JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/ ...

  9. redis内存模型及应用解读

    Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型:字符串.哈希 ...

  10. 撩课-Java每天5道面试题第20天

    131.Spring是什么? 他解决的是业务逻辑层和其他各层的松耦合问题, 因此它将面向接口的编程思想 贯穿整个系统应用. Spring是一个轻量级的IoC和AOP容器框架. 目的是解决企业应用开发的 ...