所以我尝试将一些回收站ViewHolders重构为ConstraintLayouts。完成之后,我被看到的东西震惊了。放大单个 View 所需的时间比通常的LinearLayout多20倍。实际上,这样做时会跳过很多帧。

编辑:约束布局的版本不相关。尝试过的不同组合具有几乎相同的结果。

谁能解释为什么会这样?
也许它不是为这样的“沉重” View 设计的?

这是XML中使用的根ViewHolder:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:elevation="@dimen/param_2"
    android:orientation="vertical"
    android:stateListAnimator="@animator/material_selector">

    <LinearLayout
        android:id="@+id/order_view_tabs_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/param_2"
        android:padding="@dimen/param_4"
        android:background="@color/white"
        android:divider="@drawable/empty_horizontal_divider"
        android:elevation="@dimen/param_2"
        android:orientation="horizontal"
        android:showDividers="middle"
        android:visibility="gone"/>

    <include layout="@layout/order_list_item_constraint"/>

    </LinearLayout>

这是order_list_item_constraint.xml
<android.support.constraint.ConstraintLayout
    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="wrap_content"
    android:padding="@dimen/param_4"
    android:clipToPadding="false">

    <TextView
        android:id="@+id/delivery_status"
        style="@style/DefaultText.Normal"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginEnd="4dp"
        android:padding="4dp"
        android:background="@color/white"
        android:elevation="2dp"
        android:gravity="center_vertical"
        android:text="@string/main_swipe_list_item_info_title_delivered_time"
        app:layout_constraintEnd_toStartOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="4dp"/>

    <TextView
        android:id="@+id/order_list_item_order_title"
        style="@style/FullListItemInfoText"
        android:layout_width="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/main_swipe_list_item_info_title_order"
        android:textColor="@color/red_900"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/delivery_status"/>

    <TextView
        android:id="@+id/order_list_item_order_id"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@+id/start_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_order_title"
        />

    <TextView
        android:id="@+id/order_list_item_price_title"
        style="@style/FullListItemInfoText"
        android:layout_width="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/main_swipe_list_item_info_title_sum"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_order_id"
        />

    <TextView
        android:id="@+id/order_list_item_price"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@+id/start_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_price_title"
        />


    <TextView
        android:id="@+id/order_list_item_threshold_title"
        style="@style/FullListItemInfoText"
        android:layout_width="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/order_full_list_item_threshold_value_title"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_price"
        />

    <TextView
        android:id="@+id/order_list_item_threshold_value"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@+id/start_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_threshold_title"
        />


    <TextView
        android:id="@+id/order_list_item_sl_title"
        style="@style/FullListItemInfoText"
        android:layout_width="0dp"
        android:layout_marginTop="@dimen/param_4"
        android:text="@string/main_swipe_list_item_info_title_service_level"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_threshold_value"/>


    <TextView
        android:id="@+id/order_list_item_service_level_title"
        style="@style/FullListItemInfoDetailsText"
        android:textStyle="bold"
        android:layout_width="0dp"
        android:textSize="@dimen/text_size_12"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_sl_title"/>

    <TextView
        android:id="@+id/order_list_item_service_level_try_on"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        android:textSize="@dimen/text_size_12"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_service_level_title"/>


    <TextView
        android:id="@+id/order_list_item_service_level_partial_purchase"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        android:textSize="@dimen/text_size_12"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/order_list_item_service_level_try_on"/>


    <com.express.mobile.customView.MyNetworkImageView
        android:id="@+id/order_list_item_image_map"
        android:layout_width="0dp"
        android:layout_height="144dp"
        android:elevation="2dp"
        android:scaleType="centerCrop"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/mid_guideline"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/order_list_item_map_pin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/param_30"
        android:contentDescription="@null"
        android:elevation="2dp"
        android:src="@drawable/ic_map_pin_sz_1"
        android:visibility="gone"
        app:layout_constrainedHeight="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@id/order_list_item_image_map"
        app:layout_constraintEnd_toEndOf="@id/order_list_item_image_map"
        app:layout_constraintStart_toStartOf="@id/order_list_item_image_map"
        app:layout_constraintTop_toTopOf="@id/order_list_item_image_map"/>


    <include
        android:id="@+id/order_list_item_map_interval_box"
        layout="@layout/map_interval_box"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/param_48"
        app:layout_constrainedWidth="true"
        app:layout_constraintStart_toStartOf="@id/order_list_item_image_map"/>

    <TextView
        android:id="@+id/order_list_item_timer"
        style="@style/WhiteText.Large"
        android:textStyle="bold"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/order_full_list_item_delivery_status_box_borders"
        android:elevation="2dp"
        android:gravity="center"
        android:text="@string/timer_zero_time_value_text"
        android:textSize="@dimen/text_size_24"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@id/order_list_item_map_interval_box"
        app:layout_constraintEnd_toEndOf="@id/order_list_item_map_interval_box"
        app:layout_constraintStart_toStartOf="@id/order_list_item_map_interval_box"
        app:layout_constraintTop_toTopOf="@id/order_list_item_map_interval_box"/>


    <ImageView
        android:id="@+id/order_list_item_partner_icon"
        android:layout_width="@dimen/param_40"
        android:layout_height="@dimen/param_40"
        android:layout_margin="4dp"
        android:background="@drawable/order_mod_icon"
        android:backgroundTint="@color/red_800"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:scaleType="center"
        android:src="@drawable/ic_partner"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/order_list_item_image_map"
        app:layout_constraintStart_toStartOf="@+id/order_list_item_image_map"/>

    <ImageView
        android:id="@+id/order_list_item_prepaid_icon"
        android:layout_width="@dimen/param_40"
        android:layout_height="@dimen/param_40"
        android:layout_margin="4dp"
        android:background="@drawable/order_mod_icon"
        android:backgroundTint="@color/green_800"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:scaleType="center"
        android:src="@drawable/ic_prepaid"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/order_list_item_image_map"
        app:layout_constraintStart_toEndOf="@id/order_list_item_partner_icon"/>

    <ImageView
        android:id="@+id/order_list_item_microcredit_icon"
        android:layout_width="@dimen/param_40"
        android:layout_height="@dimen/param_40"
        android:layout_margin="4dp"
        android:background="@drawable/order_mod_icon"
        android:backgroundTint="@color/blue_grey_700"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:scaleType="center"
        android:src="@drawable/ic_microcredit"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/order_list_item_image_map"
        app:layout_constraintStart_toEndOf="@id/order_list_item_prepaid_icon"/>


    <ImageView
        android:id="@+id/order_list_item_ongoing_icon"
        android:layout_width="@dimen/param_40"
        android:layout_height="@dimen/param_40"
        android:layout_margin="4dp"
        android:background="@drawable/order_mod_icon"
        android:backgroundTint="@color/colorPrimaryDark"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:scaleType="center"
        android:src="@drawable/ic_delivery_time_ongoing"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/order_list_item_image_map"
        app:layout_constraintStart_toEndOf="@id/order_list_item_microcredit_icon"/>


    <TextView
        android:id="@+id/order_list_item_name"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        android:layout_height="@dimen/param_20"
        android:layout_marginTop="8dp"
        android:drawablePadding="@dimen/param_8"
        android:drawableStart="@drawable/ic_man"
        android:maxLines="1"
        app:layout_constraintEnd_toStartOf="@id/order_list_item_call_icon"
        app:layout_constraintStart_toStartOf="@id/mid_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_image_map"/>

    <TextView
        android:id="@+id/order_list_item_phone"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        android:layout_height="@dimen/param_20"
        android:layout_marginStart="@dimen/param_28"
        android:maxLines="1"
        app:layout_constraintEnd_toStartOf="@id/order_list_item_call_icon"
        app:layout_constraintStart_toStartOf="@id/mid_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_name"/>

    <ImageView
        android:id="@+id/address_icon"
        android:layout_width="@dimen/param_20"
        android:layout_height="@dimen/param_20"
        android:layout_marginTop="4dp"
        android:contentDescription="@null"
        android:src="@drawable/ic_address"
        app:layout_constraintStart_toStartOf="@id/mid_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_phone"/>

    <TextView
        android:id="@+id/order_list_item_address"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_marginTop="4dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="4dp"
        android:minLines="2"
        app:layout_constraintEnd_toStartOf="@id/order_list_item_call_icon"
        app:layout_constraintStart_toEndOf="@id/address_icon"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_phone"/>


    <ImageView
        android:id="@+id/order_list_item_call_icon"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:padding="@dimen/param_8"
        android:background="@drawable/order_mod_icon"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:src="@drawable/ic_call"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/order_list_item_name"/>


    <ImageView
        android:id="@+id/order_list_item_navigate_icon"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:padding="@dimen/param_8"
        android:background="@drawable/order_mod_icon"
        android:contentDescription="@null"
        android:elevation="@dimen/param_4"
        android:src="@drawable/ic_order_navigate"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/order_list_item_address"/>


    <android.support.constraint.Guideline
        android:id="@+id/mid_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="360dp"/>


    <android.support.constraint.Guideline
        android:id="@+id/start_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="4dp"
        app:layout_constraintStart_toStartOf="parent"/>


     </android.support.constraint.ConstraintLayout>

这是问题的证明。所有 subview 的测量时间约为0.1ms
android - ConstraintLayout onMeasure非常慢-LMLPHP

比较一个简单的LinearLayout android - ConstraintLayout onMeasure非常慢-LMLPHP

EDIT2:这是带有LinearLayout的布局版本:
https://pastebin.com/ZvffUHnw

最佳答案

这绝对是意料之外的-我将不得不做更多的调查以了解造成这种情况的原因。请注意,现在1.1 beta的版本将比1.0慢,所有优化器阶段均未启用。乍看之下,很多textview的宽度为0dp,这非常昂贵-就像线性布局一样,0dp将导致双重测量。例如。代替:

<TextView
    android:id="@+id/order_list_item_order_title"
    android:layout_width="0dp"
    android:layout_height="20dp"
    android:layout_marginTop="4dp"
    android:text="@string/main_swipe_list_item_info_title_order"
    app:layout_constraintEnd_toEndOf="@id/mid_guideline"
    app:layout_constraintStart_toStartOf="@id/start_guideline"
    app:layout_constraintTop_toBottomOf="@+id/delivery_status"/>

您可以这样做:
<TextView
    android:id="@+id/order_list_item_order_title"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_marginTop="4dp"
    android:text="@string/main_swipe_list_item_info_title_order"
    app:layout_constraintStart_toStartOf="@id/start_guideline"
    app:layout_constraintTop_toBottomOf="@+id/delivery_status"/>

您在使用指南时也不需要在此处同时具有开始和结束约束。

请注意,通常来说,HierarchyViewer不会为您提供准确的度量值(也就是说,两者之间存在很大差异,这似乎有问题)。

您的com.express.mobile.customView.MyNetworkImageView自定义 View 如何处理措施?当您将其设置为0dp时,也会在您的布局中对其进行两次测量。

最后,您能否在您所包含的布局中添加order_list_item_map_interval_box中的内容?

编辑1.1 beta 6应该可以大大提高性能

09-30 23:27