首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么给Android布局充气要花这么长时间?

为什么给Android布局充气要花这么长时间?
EN

Stack Overflow用户
提问于 2018-06-22 04:57:30
回答 1查看 1.5K关注 0票数 4

我在Android中有一个对话框。当它打开时,有一个明显的滞后。以下是我所做的调查:

  1. 使用粗糙的计时与System.currentTime,我的onCreateDialog方法在好的一天需要150ms,在正常的一天需要500-700ms。
  2. 我实现了我自己的LayoutInflator.Factory。我的工厂什么也不做。它只是返回null,让默认的工厂来做这项工作。然而,它写出了自上次调用以来经过的时间。这样,我就可以打印出布局xml中的每个标记需要多长时间才能膨胀。非常令人惊讶的是,每个元素都需要大约20-70ms的充气时间!
  3. I分析了这个应用程序。似乎很多时间确实花在了视图的构造函数或LayoutParams.
  4. For上。我用System.currentTime测量了在TextView上调用构造函数的时间。在我的模拟器中,在一台强大的Alienware PC上,实例化一个TextView对象需要20-70ms!可能花了这么长时间,似乎有些不对劲。

作为参考,布局膨胀、测量和渲染之间存在差异。我现在只关心通货膨胀的表现。

下面是我如何实现LayoutInflator.Factory来进行测量:

        LayoutInflater inflater = LayoutInflater.from(getActivity());
    LayoutInflater inflater2 = inflater.cloneInContext(getActivity());
    inflater2.setFactory(new LayoutInflater.Factory() {

        long inner = System.currentTimeMillis();

        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            Log.d(LOG_TAG, "onCreateView: Called factory for " + name + " took " + (System.currentTimeMillis() - inner));
            inner = System.currentTimeMillis();
            return null;
        }
    });

下面是输出:

Called factory for TextView took 34
Called factory for TextView took 30
Called factory for android.support.constraint.Guideline took 76
...

下面是我如何测量实例化TextView和LayoutParams对象的时间。

    Log.d(LOG_TAG, "onCreateDialog: 10 " + (System.currentTimeMillis() - last));  last = System.currentTimeMillis();

    new TextView(getActivity());
    Log.d(LOG_TAG, "onCreateDialog: 11 " + (System.currentTimeMillis() - last));  last = System.currentTimeMillis();

    new ConstraintLayout(getActivity().getApplicationContext());
    Log.d(LOG_TAG, "onCreateDialog: 12 " + (System.currentTimeMillis() - last));  last = System.currentTimeMillis();

以下是我的布局,但应用程序中的所有布局似乎都受到速度缓慢的影响:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/remote_message_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/remote_dialog_message"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/building_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/remote_message_text_view"
        tools:text="Bakery"/>

    <ImageView
        android:id="@+id/building_icon_image_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/building_icon_content_description"
        app:layout_constraintEnd_toEndOf="@+id/right_guide_line"
        app:layout_constraintStart_toStartOf="@+id/left_guide_line"
        app:layout_constraintTop_toBottomOf="@+id/building_name_text_view"
        tools:src="@drawable/bakery"/>

    <TextView
        android:id="@+id/action_heading_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:paddingTop="8dp"
        android:text="@string/actions_heading"
        android:textAllCaps="true"
        android:textColor="#000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/building_icon_image_view"/>

    <android.support.v7.widget.GridLayout
        android:id="@+id/action_grid_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        app:columnCount="3"
        app:layout_constraintTop_toBottomOf="@+id/action_heading_text_view"
        tools:layout_height="100dp"/>

    <TextView
        android:id="@+id/production_rules_heading_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:paddingTop="8dp"
        android:text="@string/production_rules_heading"
        android:textAllCaps="true"
        android:textColor="#000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/action_grid_layout"/>

    <TextView
        android:id="@+id/production_rules_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/production_rules_heading_text_view"
        tools:text="1 flour creates 1 bread."/>

    <TextView
        android:id="@+id/production_speed_heading_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:paddingTop="8dp"
        android:text="@string/production_speed_heading"
        android:textAllCaps="true"
        android:textColor="#000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/production_rules_text_view"/>

    <TextView
        android:id="@+id/production_speed_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/production_speed_heading_text_view"
        tools:text="1 peasant produces 1 unit per 60 minutes.\n(Add a peasant to cut the time to 30 minutes.)"/>

    <TextView
        android:id="@+id/countdown_heading_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="8dp"
        android:paddingStart="8dp"
        android:paddingTop="8dp"
        android:text="@string/next_unit_count_down_heading"
        android:textAllCaps="true"
        android:textColor="#000"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/production_speed_text_view"/>

    <TextView
        android:id="@+id/countdown_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000"
        android:textSize="32sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/countdown_heading_text_view"
        tools:text="24 : 60 : 60"/>

    <android.support.constraint.Guideline
        android:id="@+id/left_guide_line"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="10dp"
        app:layout_constraintGuide_percent=".33"/>

    <android.support.constraint.Guideline
        android:id="@+id/right_guide_line"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".66"/>

</android.support.constraint.ConstraintLayout>

为什么充气布局如此缓慢。我怎么才能加快速度呢?

EN

回答 1

Stack Overflow用户

发布于 2018-06-22 05:01:14

创建视图只是LayoutInflater所做工作的一小部分。您没有初始化它们,您没有将它们添加到它们的父级,您没有在它们上设置属性,您没有创建LayoutParams,您没有进行布局或度量传递。您甚至没有解析xml。当你做10%的工作时,需要10%的时间。你的比较是完全无用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50977323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档