android布局优化和绘制优化

来源:互联网 发布:淘宝联盟分享赚互刷 编辑:程序博客网 时间:2024/05/23 13:54

一、下面介绍几种android布局优化方法:

1、include标签:

可以允许在一个布局中引入另外一个布局。比如说:我们程序的所有界面都有一个公共部分,这个时候这个公共部分就可以提取到一个独立的布局文件中,然后每个布局文件来引用这个公共的布局。(标题栏)

1.<?xml version="1.0" encoding="utf-8"?>  2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  3.    android:layout_width="match_parent"  4.    android:layout_height="match_parent"  5.    android:orientation="vertical" >  6.  7.    <include layout="@layout/titlebar" />  8.  9.    ......  10.  </LinearLayout> 


2、merge标签:

merge标签时作为<include>标签的一种辅助扩展来使用的,它的主要作用是:为了防止include引用布局文件时,产生多余的布局嵌套。

  <?xml version="1.0" encoding="utf-8"?>    <merge xmlns:android="http://schemas.android.com/apk/res/android">            <Button            android:id="@+id/ok"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:text="OK" />            <Button            android:id="@+id/cancel"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_marginTop="10dp"            android:text="Cancel" />        </merge> 


3、ViewStub标签:

仅在需要时才加载布局。ViewStub虽是view的一种,但是它是没有大小,没有绘制,也不参与布局的,所以它的绘制是不会影响布局的。

  <?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:orientation="vertical" >            <EditText            android:id="@+id/edit"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_marginTop="10dp"            android:hint="@string/edit_something_here" />            <Button            android:id="@+id/more"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:layout_marginRight="20dp"            android:layout_marginBottom="10dp"            android:text="More" />                <ViewStub             android:id="@+id/view_stub"            android:layout="@layout/profile_extra"            android:layout_width="match_parent"            android:layout_height="wrap_content"            />            <include layout="@layout/ok_cancel_layout" />        </LinearLayout> 

  public void onMoreClick() {        ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);        if (viewStub != null) {            View inflatedView = viewStub.inflate();            editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1);            editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2);            editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3);        }    }  

备注:ViewStub和merge是不能一起用的。


4、删除布局中 无用控件、无用层级。

5、在满足需求的情况下:LinearLayout和RelativeLayout,应采用LinearLayout。如果功能需要LinearLayout嵌套使用,那就优先使用RelativeLayout。

      为什么LinearLayout比RelativeLayout要好:因为在xml解析的时候LinearLayout只需一次measure,而RelativeLayout需要measure两次。


二、绘制优化:

View 的onDraw要避免大量操作,主要体现在两个方面:

(1)  在onDraw中,不要创建新的局部变量。因为onDraw方法可能会被频繁调用(大量的临时对象不仅占用内存,而且还会导致系统更频繁的gc,从而降低程序的执行效率)。

(2)  onDraw方法中不要做耗时操作,也不能做成千上万的循环操作,因为它们会导致view的绘制不流畅。



0 0