【问题标题】:Wrong draw time in the Hierarchy Viewer?层次查看器中的绘制时间错误?
【发布时间】:2011-06-28 22:14:45
【问题描述】:

我正在使用 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>

SlowDrawView 扩展了 TextView,但我修改了 onDraw(),如下所示:

@Override
protected void onDraw(Canvas canvas) {
    try {
        Thread.sleep(3000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    super.onDraw(canvas);
}

当我在 hv 中检查这个 View 的绘制时间时,我注意到小于 3segs,这没有任何意义。

对为什么会发生这种情况有任何想法吗? 我还注意到在 android git repo 中有两个版本的 hv。有谁知道区别吗?

【问题讨论】:

    标签: android android-sdk-tools


    【解决方案1】:

    看起来层次结构查看器在其测量中使用“线程时间”而不是“挂钟时间”。这意味着它不会一直计算线程正在等待来自另一个线程的某些结果(阻塞它)或线程休眠时间,如您的示例。

    我在 Android CPU Monitor 中检查了它,它证实了我的想法。

    您可以通过一些繁重的操作加载主线程,以查看线程时间如何增加。我从onDraw()onLayout() 调用了39 号的递归斐波那契算法:

     @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);
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多