Android性能优化—布局优化技巧

本文探讨了Android应用性能优化中的布局优化,包括减少布局层次以加快渲染速度,重用布局文件如的使用,以及通过TextView优化显示文字和图片。此外,还介绍了ViewStub的延迟加载、线性布局自带的分割线和Space控件的运用,以减少资源消耗,提高应用性能。
摘要由CSDN通过智能技术生成

前面几篇文章在前面几篇文章当中,

Android 内存泄漏和OOM分析(一)

Android 内存泄漏和OOM分析(二)

Android app 启动优化

我们学习了如何通过合理管理内存,app的优化启动的方式来提升应用程序的性能。实际上界面布局也会对应用程序的性能产生比较大的影响,如果布局写得嵌套多,重复布局多次出现,一个小的布局利用很多控件来实现的话,那么程序加载UI的速度就会非常慢,从而造成不好的用户体验。,如何通过优化布局来提供应用程序的性能???

本文通过示例,为大家讲解一下。

总体原则:少用,复用,减少嵌套!

减少布局层次,加快渲染速度

  • View的数量减少伴随着的就是层级的减少。从而达到结构清晰,渲染速度快的效果。

  • 当线性布局LinearLayout和相对布局都能使用时,优先使用线性布局LinearLayout,因为RelativeLayout会让子View调用至少2次onMeasure,LinearLayout有weight时,才会让子多次调用onMeasure。Measure的耗时越长那么绘制效率就低。(下图可以看打印日志,代码是重写控件的onMeasure,在super()下加句打印)

  • 尽量避免RelativeLayout嵌套RelativeLayout

重用布局文件

是有些时候我们可能需要反复利用某个已经写好的布局,总是使用复制粘贴的方式来进行布局重用,这显然是一种很笨的做法。而Android当然也已经充分考虑到了布局重用的重要性,于是提供了< include >和< merge >这两个非常有用的标签,下面我们来学习一下。

重用< include >

新建一个hotanddown.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

                android:layout_width="213dp"
                android:layout_height="284dp"
               >
    <TextView
        android:id="@+id/hotplayer_image"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="@drawable/hotplayer"
        android:clickable="false"
        android:focusable="false"
        android:gravity="bottom|center_horizontal"
        android:paddingBottom="15dp"

        android:textColor="@color/colorWhite"
        android:textSize="24sp"/>

    <TextView
        android:id="@+id/hotplayer_ione"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_below="@+id/hotplayer_image"
        android:background="#1D4468"
        android:clickable="false"
        android:drawableLeft="@drawable/first"
        android:drawablePadding="10dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:focusable="false"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="真正男子汉"
        android:textColor="@color/colorWhite"
        android:textSize="18sp"/>

    <ImageView
        android:id="@+id/conner_one"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/hotplayer_ione"
        android:clickable="false"
        android:focusable="false"
        android:src="@drawable/parting_line"/>

    <TextView
        android:id="@+id/hotplayer_itwo"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_below="@+id/conner_one"
        android:background="#1D4468"
        android:clickable="false"
        android:drawableLeft="@drawable/second"
        android:drawablePadding="10dp"
        android:focusable="false"
        android:maxLines="1"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="爸爸去哪儿"
        android:textColor="@color/colorWhite"
        android:textSize="18sp"/>

    
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值