您的位置:首页 > 移动开发 > Android开发

关于Android布局优化(一)

2016-03-31 14:14 381 查看
要想优化我们的布局,首先我们要了解Adnroid的UI渲染机制:

Android UI渲染机制

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

优化布局层级减少嵌套

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

使用 < include>标签重用Layout

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

" data-snippet-id="ext.4498cd63b06c2f3194eaaf6d088acc93" data-snippet-saved="false" data-codota-status="done">[code]<?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"/>


" data-snippet-id="ext.9b3c3d6f45a8b950b8418a5e45d68b00" data-snippet-saved="false" data-codota-status="done">[code]<?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属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ui 优化 布局