Android内存优化(二)--布局优化

下面讲解下Android中的布局优化:

一.减少布局文件的层次

1.RelativeLaoutLinearLayout选择LinearLayout

因为前者功能比较复杂,他的布局过程需要花费更多的CUP时间。记住了吗???----记住了...

2.LinearLayout嵌套时,能用FrageLayout替代,就用FrageLayout

因为ViewGroup的嵌套相当于增加了布局层级,同样会降低程序的性能。记住了吗???----记住了...

3.采用<include><merge>组合标签

布局参数,可以在布局中重写

布局参数,也可以在代码中写

4.采用ViewStub

ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。

 5.绘制的优化

是指,View的onDraw方法反要避免大量的操作,体现在两方面

首先,onDraw不要创建新的布局对象,因为onDraw方法可能会频繁被调用,瞬间产生大量临时对象,占用过多内存并且还会导致频繁的gc,

降低程序执行效率。

另一方面,onDraw方法中不要做耗时的任务,也不能执行成千上万次的循环操作,这样会抢占cup时间片,造成view的绘制不流畅。

二.<include><merge>组合标签:

创建一个类似topBar布局:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <RelativeLayout
        android:id="@+id/head_layout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
       >

        <LinearLayout
            android:id="@+id/head_back_ll"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
             >

            <ImageView
                android:id="@+id/head_back"
                android:layout_width="25dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="5dp"
                android:src="@drawable/back" />

            <TextView
                android:id="@+id/head_back_tv"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="退出"
                android:textSize="15dp" />
        </LinearLayout>

        <TextView
            android:id="@+id/header_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="我是标题"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/head_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:gravity="center"/>
    </RelativeLayout>

</merge>
用的地方,直接引用:

<include layout="@layout/view_header" />
三.ViewStub的一些特点:

        1. ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控制它了。

        2. ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。

           3.某些布局属性要加在ViewStub而不是实际的布局上面,才会起作用,比如上面用的android:layout_margin*系列属性,如果加在TextView上面,则不会起作用,需要放在它的ViewStub上面才会起作用。而ViewStub的属性在inflate()后会都传给相应的布局

创建ViewSub布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
  android:gravity="center_horizontal">  
  <ViewStub   
    android:id="@+id/viewstub_demo_text"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginLeft="5dip"  
    android:layout_marginRight="5dip"  
    android:layout_marginTop="10dip"  
    android:layout="@layout/viewstub_demo_text_layout"/>  
  <ViewStub   
    android:id="@+id/viewstub_demo_image"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginLeft="5dip"  
    android:layout_marginRight="5dip"  
    android:layout="@layout/viewstub_demo_image_layout"/>  
</LinearLayout>  
TextView的布局:


[html] view plaincopy 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content">  
    <ImageView  
        android:id="@+id/viewstub_demo_imageview"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"/>  
</LinearLayout> 
ImageView布局:


[html] view plaincopy 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content">  
    <ImageView  
        android:id="@+id/viewstub_demo_imageview"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"/>  
</LinearLayout> 

下面来看代码,决定来显示哪一个,只需要找到相应的ViewStub然后调用其infalte()就可以获得相应想要的布局:

[java] view plaincopy 
package com.effective;  

import android.app.Activity;  
import android.os.Bundle;  
import android.view.ViewStub;  
import android.widget.ImageView;  
import android.widget.TextView;  

public class ViewStubDemoActivity extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.viewstub_demo_activity);  
        if ((((int) (Math.random() * 100)) & 0x01) == 0) {  
            // to show text  
            // all you have to do is inflate the ViewStub for textview  
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);  
            stub.inflate();  
            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);  
            text.setText("The tree of liberty must be refreshed from time to time" +  
                    " with the blood of patroits and tyrants! Freedom is nothing but " +  
                    "a chance to be better!");  
        } else {  
            // to show image  
            // all you have to do is inflate the ViewStub for imageview  
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);  
            stub.inflate();  
            ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);  
            image.setImageResource(R.drawable.happy_running_dog);  
        }  
    }  
}  

使用起来都还比较方便,内存优化,我们只能按照文档,和大牛总结的来参考,毕竟这东西,直接测不出来。

在实际开发中,出现了OOM那么,就需要检查我们的程序,一点点排除,手机性能不同,出现的节点也会不同.....






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章