android布局性能优化

本质是通过复用控件达到性能优化。

1、通过<Include/>

被复用控件:

<?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="wrap_content"
    android:orientation="vertical"
    android:background="#ccc"
    >
    <TextView
        android:gravity="center"
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="使用include复用控件"
    android:textSize="15sp"
    android:textColor="@android:color/holo_red_light"
        />

</LinearLayout>

<LinearLayout 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=".MainActivity"
    android:orientation="vertical"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <QuickContactBadge
        android:id="@+id/quickContactBadge1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="48dp"
        android:layout_toRightOf="@+id/textView1" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/quickContactBadge1"
        android:layout_below="@+id/quickContactBadge1"
        android:layout_marginRight="34dp"
        android:layout_marginTop="37dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_below="@+id/progressBar1"
        android:layout_marginTop="26dp"
        android:text="Button" />
    <include layout="@layout/bottom"
        android:id="@+id/myresue"
       android:layout_alignParentBottom="true"

        />

</LinearLayout>

复用效果:

2‘通过Merge

解决布局嵌套降低性能的问题。把一个布局中处在同意层次的控件抽取出来,在include,提高性能。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:layout_gravity="center_vertical"
     >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button2" />

</merge>
<?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="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#f00"
        >
         <TextView
        android:gravity="center"
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="使用Merge复用控件"
    android:textSize="15sp"
    android:textColor="@android:color/holo_red_light"
        />

    </LinearLayout>
     <LinearLayout 

        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
      android:background="#0f0"

        >
            <TextView
        android:gravity="center"
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="使用Merge复用控件"
    android:textSize="15sp"
    android:textColor="@android:color/holo_red_light"
        />

     </LinearLayout>

      <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
      android:background="#00f"

        >
            <TextView
        android:gravity="center"
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="使用Merge复用控件"
    android:textSize="15sp"
    android:textColor="@android:color/holo_red_light"
        /> 

      </LinearLayout>
      <LinearLayout
            android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
          >
          <include layout="@layout/mybutton"/>
      </LinearLayout>

</LinearLayout>

效果:

时间: 2024-07-30 10:23:43

android布局性能优化的相关文章

Android布局性能优化—从源码角度看ViewStub延迟加载技术

在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码中通过控制View.VISIABLE动态的更改它的可见性.这样的做法的优点是逻辑简单而且控制起来比较灵活.但是它的缺点就是,耗费资源,虽然把View的初始可见View.GONE但是在Inflate布局的时候View仍然会被Inflate,也就是说仍然会创建对象,会被实例化,会被设置属性.也就是说,会

Android App性能优化(一)之布局优化

当创建复杂布局的时候,我们会在xml 文件中添加大量的ViewGroup和View.伴随着每次迭代,View树的层次越来越深,界面加载速度越来越慢,消耗的内存也越来越多.当您的程序出现加载时短暂黑屏或横竖切换时短暂黑屏,抑或如内存溢出(OOM)之类的问题时,没准您的程序需要优化了. 那么如何让程序运行速度更快?响应更敏捷?优化布局是一个最基本的方法,本文将介绍最基本的优化布局方法. 1.使用ViewStub实现View的延迟加载. 很多情况下,xml布局文件中的部分View初始状态是设置为不显示

Android开发性能优化总结(一)

安卓开发应用首先要讲究良好的用户体验,如果一款软件卡顿现象严重,不流畅,经常崩溃,那么将给用户带来极不良好的体验,从而损失用户. 在实际开发和学习中,我总结了一下关于安卓性能的优化,供大家参考交流. 应用程序的性能问题体现在很多方面, 比如第一次启动速度慢,或者进入某一界面速度慢:动画执行过程不流畅,或者动画执行卡顿时间长:ListView列表滑动过程中卡顿,不流畅:应用程序自定义的某特定界面执行速度慢:响应某一用户事件时长时间无响应(ANR):操作数据库时,执行大量数据的增删改查操作,执行速度

Android客户端性能优化(魅族资深工程师毫无保留奉献)

本文由魅族科技有限公司资深Android开发工程师degao(嵌入式企鹅圈原创团队成员)撰写,是degao在嵌入式企鹅圈发表的第一篇原创文章,毫无保留地总结分享其在领导魅族多个项目开发中的Android客户端性能优化经验,极具实践价值! 即日起,嵌入式企鹅圈将在之前五个专栏(Linux内核驱动情景分析.资源紧缺型SOC嵌入式架构设计.嵌入式交叉工具链及其应用.嵌入式设计和编程.微信硬件平台和物联网解决方案)新增Android开发专栏!更多Android.Linux.嵌入式和物联网原创技术分享敬请

android App性能优化技巧浅谈

Android App性能优化,安卓App性能优化技巧,无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的.那么,如何能开发出更高性能的Android App?相信是软件开发公司以及广大程序员们头疼的一大难题.今天,就给大家提供几个提高Android App性能的技巧. 高效地利用线程1.在后台取消一些线程中的动作 我们知道App运行过程中所有的操作都默认在主线程(UI线程)中进行的,这样App的响应速度就会受

Android应用性能优化

遇到的问题: 1)ANR 2)ListView 卡顿,不流畅 3)Activity启动慢 4)动画不流畅,启动前卡顿 5)自定义view启动慢 6)  OOM 7)数据库大量操作 8)长时间运行后,程序变慢 基本思想: 1)语言层解决问题,语法上提高性能 2)合理的数据结构和算法 3)布局优化,布局深度控制 4)工作线程与UI线程分离 5)合理的缓存机制 6)NDK合理使用 7)优化的SQL语句 8)使用工具,分析问题找出瓶颈 优化工具: view优化工具:hierarchy view 代码优化

Android开发性能优化总结(二)

接上一篇<Android开发性能优化总结(一)> 一.安卓UI性能检测与优化 UI是安卓应用程序与用户打交道的最直接途径,UI设计的好不好,直接影响到用户的体验,如果没有达到他们心目中的自然流畅细节,用户要是能够感觉出来,少则影响心情,多则卸载应用:所以一个应用的UI显示性能问题就不得不被开发人员重视. 1.UI卡顿常见原因: 在UI线程中做了耗时操作,导致UI线程卡顿: 布局Layout过于复杂,无法在16ms内完成渲染: 同一时间动画执行的次数过多,导致CPU或GPU负载过重: View过

Android app 性能优化之视图优化

Android app 性能优化之视图优化 前言: 每当一款App开始快速扩展的时候,随着业务功能的越来越复杂,功能模块的越来越多总会引起这样那样的性能问题.交互不流畅卡顿,ANR,手机发热量大等等性能问题在Android开发中一直都是一个坑爹的存在.不是大家不想去优化,可能是等你发现开始要搞搞性能的时候,发现工程貌似好大了,能跑不崩就万事大吉了,那么多代码要去看,去改.真心是一个让人想想都糟心的事.也可能,关键点不好找,可能一个性能问题是别的你想都想不到的地方引起的,为了优化这么一个点,可能花

Android APP 性能优化的一些思考

说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才感觉运行速度稍微提高了点,就算手机在各种性能跑分软件面前分数遥遥领先,还是感觉无论有多大的内存空间都远远不够用.相信每个使用 Android 系统的用户都有过以上类似经历,确实,Android 系统在流畅性方面不如 IOS 系统,为何呢,明明在看手机硬件配置上时,Android 设备都不会输于 IO