Hierarchy Viewer是Android自带的、检测UI的可视化工具,它可以检测UI的耗时。
上干货。
使用方法:
打开Hierarchy Viewer
打开虚拟机,运行你的程序
黑屏很久,等一会儿就好了
结果如图:
如何看懂?
使用起来很简单,但是如何运用到实际上呢?怎么知道什么样的UI耗时,什么样的UI不耗时呢?
举个栗子:
两个button(除了宽高,其他属性都一致),那么谁更快一些呢?
<Button
android:text="100dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="91dp"
android:layout_marginStart="91dp"
android:layout_marginTop="51dp"
android:id="@+id/button" />
<Button
android:text="wrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="91dp"
android:layout_marginStart="91dp"
android:layout_marginTop="51dp"
android:id="@+id/button2" />
固定宽高:测量快,布局慢,画图快
自适应宽高:测量慢,布局快,画图慢
另外,自定义View来减少view的层次,也可以减少UI耗时
举个栗子:
自定义的搜索框布局和拼凑起来的布局(内容都是一样的)
直接上结果,0.663ms < 0.854+0.136,自定义View省时
自定义View:测量快,布局快,画图快
拼凑VIew:测量慢,布局慢,画图慢
测试UI耗时的过程就是这样咯。
若有高见,不吝赐教。