Android - 优化

一、布局优化

A、利用手机的开发者选项中的一些工具(不同的设备,功能会有不同),可以对布局进行优化。
1、开发者选项 -- 调试GPU过度渲染 -- 显示过度渲染区域
如下边的代码:
<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="com.example.hwgtoptimizationdemo.MainActivity" 
    android:background="@android:color/darker_gray">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:text="TextView1" />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:background="@android:color/darker_gray">
        <TextView
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="TextView2" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:background="@android:color/darker_gray">
        <LinearLayout 
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:background="@android:color/darker_gray">
	        <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="TextView3" />
    	</LinearLayout>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:background="@android:color/darker_gray">
        <LinearLayout 
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:background="@android:color/darker_gray">
	        <LinearLayout 
		        android:layout_width="match_parent"
		        android:layout_height="wrap_content"
		        android:background="@android:color/darker_gray">
		        <TextView
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="TextView4" />
	    	</LinearLayout>
	    </LinearLayout>
    </LinearLayout>
</LinearLayout>

对应的界面为:

从TextView1到TextView4,颜色逐渐加深,应尽量避免出现颜色较深即渲染过度的情况。
2、开发者选项 -- 显示布局边界,看下边这两段代码:
<!-- 第一段 -->
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="41dp"
    android:layout_marginTop="13dp"
    android:layout_marginLeft="23dp"
    android:layout_marginRight="23dp">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:text="姓名"/>
    <EditText 
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
<!-- 第二段 -->
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="41dp"
    android:layout_marginTop="13dp">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="23dp"
        android:layout_centerVertical="true"
        android:text="姓名"/>
    <EditText 
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="23dp"/>
</RelativeLayout>

他们对应的界面是这样的:

乍一看似乎没什么区别,但是,当我们勾选了 开发者选项 -- 显示布局边界 之后,他们对应的界面区别就大了:

如果一个界面的控件比较多的时候,那么相似的部分最好使用统一的思路来进行代码编写,这样给人的感觉是思路清晰,容易维护。
B、优化布局的层级
系统在测量、布局和绘制时,都是通过遍历view树来实现的,所以一个布局中,view树的层级不应该太多,这就需要在选择LinearLayout、RelativeLayout或是其他布局时进行综合衡量。
C、使用<include>标签引入公用的layout
D、使用<ViewStub>标签实现view的延迟加载
如果在一个界面中,有些布局是在特定的时候才需要显示出来的,那么就可以考虑使用这种方式,而不是在界面初始化时就进行加载。当然,从显示效果上来看,使用View.GONE也是可以的,他们的区别是,使用<ViewStub>标签,只会在需要显示时,才回去加载布局,而使用View.GONE的方式,在初始化View树的时候,就已经加载完成了,所以使用<ViewStub>标签更合理
 
二、内存优化
A、Bitmap优化
B、
 
 
 
posted on 2016-04-24 13:14  快乐的码农  阅读(156)  评论(0编辑  收藏  举报