我在Android中有一个对话框。当它打开时,有一个明显的滞后。以下是我所做的调查:
作为参考,布局膨胀、测量和渲染之间存在差异。我现在只关心通货膨胀的表现。
下面是我如何实现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>
为什么充气布局如此缓慢。我怎么才能加快速度呢?
发布于 2018-06-22 05:01:14
创建视图只是LayoutInflater所做工作的一小部分。您没有初始化它们,您没有将它们添加到它们的父级,您没有在它们上设置属性,您没有创建LayoutParams,您没有进行布局或度量传递。您甚至没有解析xml。当你做10%的工作时,需要10%的时间。你的比较是完全无用的。
https://stackoverflow.com/questions/50977323
复制相似问题