Android 由30多个视图组成的自定义视图的缓慢渲染
我有一个自定义视图,其中包含两行,每个行有4个其他自定义视图,并且在这8个自定义视图的每个视图周围都有边框 在xml中,我使用的是Android 由30多个视图组成的自定义视图的缓慢渲染,android,xml,android-constraintlayout,Android,Xml,Android Constraintlayout,我有一个自定义视图,其中包含两行,每个行有4个其他自定义视图,并且在这8个自定义视图的每个视图周围都有边框 在xml中,我使用的是ConstraintLayout,第一行和第二行有两个不可见的视图,这有助于使视图具有相同的宽度-s和高度-s 设计是伟大的工作,但让我烦恼的是,渲染太慢。我尝试删除视图的约束,如果没有约束,它的运行速度似乎更快 我的问题是:有没有办法提高这个视图的性能,或者这是正确的方法 下面是我使用的xml: <?xml version="1.0" encoding="u
ConstraintLayout
,第一行和第二行有两个不可见的视图,这有助于使视图具有相同的宽度
-s和高度
-s
设计是伟大的工作,但让我烦恼的是,渲染太慢。我尝试删除视图的约束
,如果没有约束,它的运行速度似乎更快
我的问题是:有没有办法提高这个视图的性能,或者这是正确的方法
下面是我使用的xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/first_row"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="4:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/all_background_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_all"
android:visibility="invisible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintEnd_toStartOf="@id/stay_filter_view"
app:layout_constraintStart_toStartOf="@id/first_row"
app:layout_constraintTop_toTopOf="@id/first_row"
app:layout_constraintBottom_toBottomOf="@id/first_row"/>
<com.my.app.presentation.component.FilterObjectTypeIconlessView
android:id="@+id/all_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_all"
app:layout_constraintStart_toStartOf="@id/all_background_filter_view"
app:layout_constraintEnd_toEndOf="@id/all_background_filter_view"
app:layout_constraintTop_toTopOf="@id/all_background_filter_view"
app:layout_constraintBottom_toBottomOf="@id/all_background_filter_view"/>
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/stay_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view"
app:layout_constraintStart_toEndOf="@id/all_background_filter_view"
app:layout_constraintTop_toTopOf="@id/first_row"
app:layout_constraintBottom_toBottomOf="@id/first_row"
custom:src="@drawable/icon__accomodation__x3"
custom:text="@string/filter_stay" />
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/restaurants_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_restaurants"
custom:src="@drawable/icon__restaurant__x3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toEndOf="@id/stay_filter_view"
app:layout_constraintEnd_toStartOf="@id/shops_filter_view"
app:layout_constraintTop_toTopOf="@id/first_row"
app:layout_constraintBottom_toBottomOf="@id/first_row" />
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/shops_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_shops"
custom:src="@drawable/icon__shop__x3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toEndOf="@id/restaurants_filter_view"
app:layout_constraintTop_toTopOf="@id/first_row"
app:layout_constraintBottom_toBottomOf="@id/first_row"
app:layout_constraintEnd_toEndOf="@id/first_row"/>
<android.support.constraint.ConstraintLayout
android:id="@+id/second_row"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="4:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/first_row" />
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/sights_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_sights"
custom:src="@drawable/icon__sights__x3"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="@id/second_row"
app:layout_constraintEnd_toStartOf="@id/sports_filter_view"
app:layout_constraintTop_toTopOf="@id/second_row"
app:layout_constraintBottom_toBottomOf="@id/second_row"
/>
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/sports_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_sports"
custom:src="@drawable/icon__sport__x3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toEndOf="@id/sights_filter_view"
app:layout_constraintEnd_toStartOf="@id/educational_filter_view"
app:layout_constraintTop_toTopOf="@id/second_row"
app:layout_constraintBottom_toBottomOf="@id/second_row"/>
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/educational_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_educational"
custom:src="@drawable/icon__study__x3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toEndOf="@id/sports_filter_view"
app:layout_constraintEnd_toStartOf="@id/public_filter_view"
app:layout_constraintTop_toTopOf="@id/second_row"
app:layout_constraintBottom_toBottomOf="@id/second_row"/>
<com.my.app.presentation.component.FilterObjectTypeWithImageView
android:id="@+id/public_filter_view"
android:layout_width="0dp"
android:layout_height="0dp"
custom:text="@string/filter_public"
custom:src="@drawable/icon__public__x3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="@id/second_row"
app:layout_constraintTop_toTopOf="@id/second_row"
app:layout_constraintStart_toEndOf="@id/educational_filter_view"
app:layout_constraintBottom_toBottomOf="@id/second_row"/>
<View
android:id="@+id/all_filter_top_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/all_filter_view"
app:layout_constraintEnd_toStartOf="@id/stay_filter_view"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/all_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/all_filter_view"
app:layout_constraintEnd_toStartOf="@id/stay_filter_view"
app:layout_constraintTop_toBottomOf="@id/all_filter_view"/>
<View
android:id="@+id/stay_filter_top_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/stay_filter_view"
app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view"
app:layout_constraintTop_toTopOf="@id/stay_filter_view"/>
<View
android:id="@+id/stay_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/stay_filter_view"
app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view"
app:layout_constraintTop_toBottomOf="@id/stay_filter_view"/>
<View
android:id="@+id/restaurants_filter_top_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/restaurants_filter_view"
app:layout_constraintEnd_toStartOf="@id/shops_filter_view"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/restaurants_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/restaurants_filter_view"
app:layout_constraintEnd_toStartOf="@id/shops_filter_view"
app:layout_constraintTop_toBottomOf="@id/restaurants_filter_view"/>
<View
android:id="@+id/shops_filter_top_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/shops_filter_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/shops_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/shops_filter_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/shops_filter_view"/>
<View
android:id="@+id/sights_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/sights_filter_view"
app:layout_constraintEnd_toStartOf="@id/sports_filter_view"
app:layout_constraintBottom_toBottomOf="@id/sights_filter_view"/>
<View
android:id="@+id/sports_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/sports_filter_view"
app:layout_constraintEnd_toStartOf="@id/educational_filter_view"
app:layout_constraintBottom_toBottomOf="@id/sports_filter_view"/>
<View
android:id="@+id/educational_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/educational_filter_view"
app:layout_constraintEnd_toStartOf="@id/public_filter_view"
app:layout_constraintBottom_toBottomOf="@id/educational_filter_view"/>
<View
android:id="@+id/public_filter_bottom_border"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toStartOf="@id/public_filter_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/public_filter_view"/>
<View
android:id="@+id/all_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/all_filter_view"
app:layout_constraintBottom_toBottomOf="@id/all_filter_view"
app:layout_constraintStart_toStartOf="@id/all_filter_view"/>
<View
android:id="@+id/sights_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/sights_filter_view"
app:layout_constraintBottom_toBottomOf="@id/sights_filter_view"
app:layout_constraintStart_toStartOf="@id/sights_filter_view"/>
<View
android:id="@+id/stay_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/stay_filter_view"
app:layout_constraintBottom_toTopOf="@id/sports_filter_view"
app:layout_constraintStart_toStartOf="@id/stay_filter_view"/>
<View
android:id="@+id/sports_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/sports_filter_view"
app:layout_constraintBottom_toBottomOf="@id/sports_filter_view"
app:layout_constraintStart_toStartOf="@id/sports_filter_view"/>
<View
android:id="@+id/restaurants_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/restaurants_filter_view"
app:layout_constraintBottom_toTopOf="@id/educational_filter_view"
app:layout_constraintStart_toStartOf="@id/restaurants_filter_view"/>
<View
android:id="@+id/educational_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/educational_filter_view"
app:layout_constraintBottom_toBottomOf="@id/educational_filter_view"
app:layout_constraintStart_toStartOf="@id/educational_filter_view"/>
<View
android:id="@+id/shops_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/shops_filter_view"
app:layout_constraintBottom_toTopOf="@id/public_filter_view"
app:layout_constraintStart_toStartOf="@id/shops_filter_view"/>
<View
android:id="@+id/public_filter_left_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/public_filter_view"
app:layout_constraintBottom_toBottomOf="@id/public_filter_view"
app:layout_constraintStart_toStartOf="@id/public_filter_view"/>
<View
android:id="@+id/shops_filter_right_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/shops_filter_view"
app:layout_constraintBottom_toBottomOf="@id/shops_filter_view"
app:layout_constraintEnd_toEndOf="@id/shops_filter_view"/>
<View
android:id="@+id/public_filter_right_border"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintTop_toTopOf="@id/public_filter_view"
app:layout_constraintBottom_toBottomOf="@id/public_filter_view"
app:layout_constraintEnd_toEndOf="@id/public_filter_view"/>
<View
android:id="@+id/first_border_center_pixel"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toEndOf="@id/all_filter_bottom_border"
app:layout_constraintBottom_toBottomOf="@id/all_filter_bottom_border" />
<View
android:id="@+id/second_border_center_pixel"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toEndOf="@id/stay_filter_bottom_border"
app:layout_constraintBottom_toBottomOf="@id/stay_filter_bottom_border" />
<View
android:id="@+id/third_border_center_pixel"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintStart_toEndOf="@id/restaurants_filter_bottom_border"
app:layout_constraintBottom_toBottomOf="@id/restaurants_filter_bottom_border" />
<View
android:id="@+id/fourth_border_center_pixel"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="@color/filterUnactivatedColor"
app:layout_constraintEnd_toEndOf="@id/shops_filter_bottom_border"
app:layout_constraintBottom_toBottomOf="@id/shops_filter_bottom_border" />
</android.support.constraint.ConstraintLayout>
优化布局的方法多种多样。首先,嵌套的
ConstraintLayout
是个坏主意
1)您应该删除所有边框视图,创建形状,并将其设置为带有图像视图的过滤器对象类型的背景。删除26个视图是一个巨大的胜利,您的布局将更具可读性
2)您可以使用3条垂直引导线,而不是使用嵌套约束布局。使用百分比值25%、50%、75%并将所有视图约束到约束。您的自定义视图将是屏幕的1/4,app:layout\u constraintDimensionRatio=“1:1”
将具有相同的高度
最后,您将有3个指南行
和4个自定义视图,这将更快、更简单布局我通过应用3个垂直指南
并删除应用程序:布局约束尺寸比=“1:1”修复了性能问题
用于表格中的8项,因为此时不需要 您是否尝试过使用简单的GridView而不是ConstraintLayout?您是否在层次结构查看器
@TodorKostov中查看了布局?我忘了提到这一点,但我需要控制每个项目的边界。如我提供的图像所示,我希望在启用/禁用项目时能够更改边框的颜色。我能用GridView
复制这种行为吗?我相信这是可能的。您仍然有自定义视图,但将只有一个GridView,而不是2个ConstraintLayouts。这将降低总体布局的复杂性。