Mac*_*rse 5 android android-sdk-tools
我正在使用Android SDK附带的Hierarchy Viewer工具进行一些测试.
在Activity我正在测试有以下XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_alignParentTop="true"/>
<TextView android:id="@+id/slow_measure"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Slow Measure"
android:layout_alignParentBottom="true"/>
<TextView android:id="@+id/slow_layout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Slow Layout"
android:layout_above="@id/slow_measure"/>
<com.test.SlowDrawView android:id="@+id/slow_draw"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#FF0000"
android:text="Slow Draw"
android:layout_above="@id/slow_layout"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在SlowDrawView扩展TextView,但我修改了onDraw()这样的:
@Override
protected void onDraw(Canvas canvas) {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.onDraw(canvas);
}
Run Code Online (Sandbox Code Playgroud)
当我View在hv中检查它的绘制时间时,我注意到小于3segs,这没有任何意义.
有关为什么会发生这种情况的任何想法?我也注意到在android git repo中有两个版本的hv.谁知道差异?
看起来层次结构查看器在其测量中使用“线程时间”而不是“挂钟时间”。这意味着它不会计算线程等待另一个线程的某些结果(阻塞它)的所有时间或线程休眠时间,如您的示例所示。
我在Android CPU Monitor中检查了它,它证实了我的想法。
您可以通过一些繁重的操作加载主线程,以查看线程时间如何增加。onDraw()我通过调用 和中的数字 39 的递归斐波那契算法来做到这一点onLayout():
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
fib(39);
}
public static int fib(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Run Code Online (Sandbox Code Playgroud)