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

本篇文章是带着大家了解Android性能的优化之布局优化,我们先总结下Android性能优化的常见方式有哪些:布局优化,ListView优化,Bitmap优化,内存泄露,响应速度优化,线程优化,绘制优化等。Android性能优化主要是针对内存和cpu的,过多的使用内存会导致程序的内存溢出(OOM),而过多使用内存的原因有很多,比如内存泄露,其会增大内存溢出的几率。过多的使用cpu资源,会造成手机卡顿甚至应用无法响应(ANR),出现的原因一般是做了大量的耗时工作导致。这篇文章只讲解布局优化,其他的方式会慢慢更新出来的,敬请期待!
布局优化的方式:
1:尽量减少布局文件中的ViewGroup的嵌套;
2:LinearLayout,FrameLayout优先于RelativeLayout使用;
3:在某一个界面的布局在多个地方相同时,用<include layout="@layout/abc">进行引用,<merge>标签经常配合使用;
4:某一个布局是选择性的出现时(例如:ListView的列表为空时,会显示一个界面来提示用户当前没有数据),可以使用控件ViewStub;
接下来,对以上四点做一个解析:
1:减少了ViewGroup的嵌套,也就减少了Android绘制界面的工作量;
2:RelativeLayout的布局过程需要cpu花费更多的时间,能直接用LinearLayout,
FrameLayout替代是最好的;
3:<include>首先减少了xml中的代码量,不必在xml文件中写重复的代码;
如果当前布局时线性布局,而被引用的布局也是线性的,那么该<LinearLayout>就是多余的,可以用<merge>标签替换<LinearLayout>,减少 ViewGroup的层级嵌套多的问题,从而减少Android绘制界面的工作量;
被引用的布局如下:
abc.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 这里原本是 LinearLayout--> 
<merge    
xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</merge>  <!-- 这里原本是 LinearLayout-->
4:处理选择性加载的布局常会在FrameLayout中一层一层显示,并动态设置可见性,如下:
<FrameLayout 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" >
	<LinearLayout
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	   	android:gravity="center">
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:visibility="invisible"
	        android:text="亲,没有数据啦"/>
	</LinearLayout>
	<ListView 
	   android:id="@+id/lv"
	   android:layout_width="match_parent"
	   android:layout_height="match_parent">
	</ListView>
</FrameLayout>
4:上面的需求可以用ViewStub替换,如下:
<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"
    tools:context=".MainActivity" >
	<ListView 
	   android:id="@+id/lv"
	   android:layout_width="match_parent"
	   android:layout_height="match_parent">
	</ListView>
	<ViewStub 
	    android:id="@+id/vb"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:layout="@layout/no_data"/>
</RelativeLayout>
no_data.xml如下:
<?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:gravity="center"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/tv_no_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="亲,没有数据啦"/>
</LinearLayout>
操作ViewStub如下
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewStub vb = (ViewStub) findViewById(R.id.vb);
		View view = vb.inflate();
		
		TextView tv_no_data = (TextView) view.findViewById(R.id.tv_no_data);
		tv_no_data.setText("亲,没有数据了,我要显示啦");
	}
}
补充,需要加载ViewStub中的布局时,可以使用两种方式:
vb.inflate();
vb.setVisibility(View.Visibility);
不加载时:
vb.setVisibility(View.GONE);
分析:ViewStub是一个轻量级的控件,只是起一个占据位置的作用,加载它只会占用很少的内存,当调用ViewStub的inflate或者setVisibility方法时,ViewStub就会被它内部的布局替代,此时ViewStub便不存在于布局结构中了。
而使用前一种帧布局的方式,控件实际是存在于布局结构中的,只是被隐藏不再显示,每次加载xml时都会加载该控件,这样会占用更多的内存资源。而使用ViewStub来占着位置,本身又很轻,不占什么内存,只需要动态加载控件进内存,可以有效的减少内存的损耗。

辛苦大家能看到这里,文章有不够准确的地方,期待大家能在下面评论中指出,一起学习进步!!!       
———小王君       (*^__^*) 





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值