布局优化:
布局性能的好坏主要体现在:页面展示速度的快慢;
影响性能的实质:页面的测量、页面的绘制;
优化思路:
布局性能、布局层次深度、布局复用、测量和绘制;
》》选择耗费性能比较少的布局;
》》减少嵌套层次;
》》提高布局的复用性;
》》减少布局的测量和绘制时间;
布局性能消耗(布局过程中消耗的cpu和时间):
耗能低的布局:FragLayout、LineraLayout;(实现功能简单)
耗能高的布局:RelativeLayout;(实现功能复杂)
》》嵌套布局消耗的性能>单个布局消耗的性能;
》》宁愿选择一个消费性能高的布局,也不采用嵌套多个性能消耗低的布局;
减少布局嵌套层级(嵌套):
布局嵌套层级越少->绘制的工作量越少->绘制的速度越快->性能就越高;
优化:选择合适的布局方式(尽量嵌套少的层级);
使用布局标签<merge>(当把有<merge>标签的布局放在<include>中的时候,就会忽视<merge>。<merge />只能作为XML布局的根标签使用。当Inflate以<merge />开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/fragment_main" />
</LinearLayout>
<merge 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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是button3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是button2" />
</merge>
选择合适的布局类型:
通过选择合适的布局方式,减少嵌套层级;
当完成一个复杂的布局是,尽量选择一个功能负责的布局,而不要选择多个功能简单的布局嵌套来完成。
提高布局的复用性(<include>):
提高代码的复用性,减少布局的测量和绘制时间(提取出布局中公共的部分进行复用);
使用布局标签<include>;
减少初次测量和绘制时间:
》》使用 布局标签<ViewStub>(按需加载外部引用布局,特定的情况下才显示,默认bu'xia)
& 尽可能少用布局属性 warp_content ;
<ViewStub/>标签最大的特点就是当你需要的时候才会加载,但并不影响UI初始化的性能。
<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">
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>
<Button
android:id="@+id/btn_gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GONE"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewStub
android:id="@+id/viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/view"/>
</LinearLayout>
</LinearLayout>
<?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">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ViewStub stub = (ViewStub) findViewById(R.id.viewstub);
stub.inflate();
》》ViewStub中的layout布局不能使用merge标签,否则会报错;
》》ViewStub的inflate只能执行一次,显示了之后,就不能再使用ViewStub控制它了;
》》与View.setVisible(View.Gone)的区别:View 的可见性设置为 gone 后,在inflate 时,该View 及其子View依然会被解析;而使用ViewStub就能避免解析其中指定的布局文件,从而节省布局文件的解析时间 & 内存的占用;
》》<ViewStub />只能inflate一次,用完以后<ViewStub />就会置为空,后期不能再次使用,重新开启应用或者杀死进程可再次使用。类似显示隐藏按钮这种功能是不能实现的。
View.GONE 费资源,虽然已经GONE掉,但显示View时还是会加载。
尽量减少用布局属性warp_content:
布局属性warp_content会增加布局测量时的计算成本,应尽可能少用(在已知宽高为固定值时,不使用wrap_content)。