Android性能优化之布局优化

Android性能优化之布局优化


)

前言

 Android中布局优化能让我们的界面绘制时间减少,用户感知到的卡顿时间也短,体验感自然而然也就上去了,Android中的布局优化可以从多个方面来入手,include,merge,ViewStub等等一些方面。

include

 我们平常在画布局的时候经常碰到一些重复的共性布局,比如说标题栏,导航栏之类的,或者一些按钮之类的,我们在使用的时候重复的写非常麻烦,增加了我们工作量,使用include标签我们就可以避免去写那些无聊的共性代码,其实这个include单独来讲和我们这一篇文章没什么关系,但是他结合merge使用就可以减少一层ViewGroup的包裹,这一个我们在merge中去讲,下面贴出include的用法。

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity">

    <include
        android:layout_width="match_parent"
        android:layout_height="50dp"
        layout="@layout/base_title_bar"/>

</LinearLayout>

merge

 merge的存在意义就是为了结合include来减少include引用过来的文件的外层ViewGroup,说白了就是用merge替代ViewGroup,虽然我们使用merge结合include可以达到减少绘制一层ViewGroup的效果,merge的布局是取决于父布局的,父布局是什么,他就是什么,或者我们可以这么理解,下面的代码我们就相当于将base_title_bar.xml中的两个textview直接写入到LinearLayout中,但是这里有个问题,我们的代码有时候include的xml与父布局所要的样式是不一样的,就比如我们当前引入的titlebar是要横向的,但是父布局是纵向的,这时候我们又得去给include外层再加上一层ViewGroup,所以我们在使用merge与include结合的时候要考虑清楚我们共性布局以后可能会使用的场景。

base_title_bar.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="out"
        android:textSize="18sp" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Title"
        android:textSize="18sp" />

</merge>
<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:orientation="vertical"
    tools:context=".MainActivity">

    <include
        android:layout_width="match_parent"
        android:layout_height="50dp"
        layout="@layout/base_title_bar"/>

</LinearLayout>

ViewStub

 ViewStub我们称之为懒加载,需要用到的时候就加载它,但是这里会有一个坑需要注意一下,就是ViewStub在显示之后就无法再隐藏了,因为ViewStub在显示的时候会将自己remove掉,替换成设置的view,这里我们只要记住ViewStub的作用就是一次性的按需加载就行了。我们使用到它的时候再按需加载,达到view在有需要的时候再渲染,以优化性能,下面是使用方法;

坑点:ViewStub中的inflate()只能调用一次,调用多次会报错

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity">


    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout="@layout/base_title_bar" />


</LinearLayout>
viewStub = (ViewStub) findViewById(R.id.viewstub);

//第一种,直接显示,不获取到view
if (viewStub != null) {
    viewStub.setVisibility(View.VISIBLE);
}

//第二种,显示,并且获取到view
if (viewStub != null) {
    View view = viewStub.inflate();//获取到的是我们要显示出来的view
}

其他的优化方式

 一些其他的优化方式就是一些细节了,需要我们平时画界面的时候自己多注意注意,并且养成习惯

1.子控件如果和父控件是同样的背景色时,子控件不需要再设定背景色了,用父控件的(能蹭一点是一点)。

2.尽量减少不必要的嵌套,我们上面的include+merge的方式只是其中一种方式,我们平时画界面的时候还是得多注意一下能减少嵌套就减少嵌套(省着点用有限的资源)

3.能用LinearLayout与FrameLayout的时候就不用RelativeLayout,毕竟RelativeLayout控件比LinearLayout与FrameLayout等复杂.

4.合理的借助工具来分析界面是否过度绘制,并且进行合理的避免

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值