关于Android布局优化(一)

要想优化我们的布局,首先我们要了解Adnroid的UI渲染机制:

Android UI渲染机制

在Android中,系统通过VSYNC信号触发对UI的渲染和重绘,时间间隔是16ms。这个16ms就是1000ms中显示60帧的单位时间。这就能解释为什么很多图片处理和画面渲染都以16ms为临界线(比如 高斯模糊处理)。如果在16ms内没有绘制完成,就会造成丢帧现象,等待下次信号到来时候才开始绘制。这就是重复同一个画面,也就是我们看到的画面卡顿现象。

优化布局层级减少嵌套

Android每次对View测量绘制的时候都是通过对View树的遍历来进行的。所以,我们要尽量降低View树的高度。新版本的Android默认使用relativelayout作为根布局,因为relativelayout的扁平化可以降低布局的嵌套层数。

使用 < include>标签重用Layout

在项目中一般为了保持风格的统一,都会有类似的布局重复出现,比如Topbar。像这样的布局就可以单独抽出来,用的时候用< include>标签来引入。

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/iv_article"

        android:layout_height="0dp"

        android:layout_width="0dp"


        android:src="@drawable/back"/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/top_bar_color"
    android:layout_height="56dp"
    android:layout_width="match_parent">

  <include layout="@layout/top_bar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent">
</RelativeLayout>

把imageView抽出来,这里我宽高都写为0dp,这样在引用时候就必须要设置宽高了。 但是要注意,如果要在< include>标签中覆盖类似源布局中的android:layout_xxxxx属性,就必须在< include>标签中同时指定
android:layout_height和android:layout_width属性。

posted @ 2016-03-31 14:14  Z漫步  阅读(186)  评论(0编辑  收藏  举报