首页 > 解决方案 > Android:提高 UI 性能

问题描述

我有一个应用程序,它在启动时显示仪表板片段。到目前为止,我只设计了 UI (XML),并且我注意到在启动时我的应用程序需要大约 1.5 秒才能打开,而我手机上的其他应用程序只需不到 0.5 秒。我认为图形渲染占用了太多内存,或者我的设计方式错误。

我的用户界面代码如下:

<?xml version="1.0" encoding="utf-8"?>

<layout 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">

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--
        Main Content
    -->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:orientation="vertical">

        <!--
            The Upper layer
        -->
        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/upper_layer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="100dp"
                android:background="@color/colorAccent">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" app:srcCompat="@drawable/img_user_colored_1"
                    android:id="@+id/imageView" app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginStart="8dp"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:srcCompat="@drawable/img_user_colored_1"
                    app:layout_constraintBottom_toTopOf="@+id/textView2"
                    app:layout_constraintVertical_chainStyle="packed" android:layout_marginTop="8dp"/>

            <TextView
                    android:text="Welcome, Mervin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2" app:layout_constraintStart_toStartOf="@+id/imageView"
                    app:layout_constraintEnd_toEndOf="@+id/imageView" android:layout_marginTop="8dp"
                    app:layout_constraintTop_toBottomOf="@+id/imageView"
                    tools:text="Title" app:layout_constraintBottom_toTopOf="@+id/textView3"
                    android:textColor="@color/colorTextSecondary"
                    style="@style/typography_h6"/>

            <TextView
                    android:text="@string/TextView_fromDashboardFragment_Title_Main"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView3"
                    tools:text="@string/TextView_fromDashboardFragment_Title_Main"
                    app:layout_constraintStart_toStartOf="@+id/imageView"
                    app:layout_constraintEnd_toEndOf="@+id/imageView"
                    app:layout_constraintTop_toBottomOf="@+id/textView2"
                    android:textColor="@color/colorTextSecondary"
                    style="@style/typography_caption"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <!--
            The lower content.
        -->

        <LinearLayout
                android:id="@+id/lower_layer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginTop="100dp"
                android:layout_marginBottom="?attr/actionBarSize"
                android:paddingBottom="16dp">

            <include
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    layout="@layout/include_forfragmentdashboard_section_goal"/>

            <include
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    layout="@layout/include_forfragmentdashboard_section_todo"/>

            <include
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    layout="@layout/include_forfragmentdashboard_section_tobuy"/>

        </LinearLayout>

    </LinearLayout>


    <!--
        The anchored CardView on the middle of the screen
    -->

    <com.google.android.material.card.MaterialCardView
            android:id="@+id/active_panel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            app:cardBackgroundColor="@color/cardView_background_default"
            app:cardElevation="@dimen/Elevation_CardView_Simple"
            app:layout_anchor="@+id/upper_layer"
            app:layout_anchorGravity="center_horizontal|bottom">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="@dimen/Padding_CardView_Simple"
                android:paddingBottom="@dimen/Padding_CardView_Simple"
                android:orientation="vertical"
                android:baselineAligned="false">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Active"
                    style="@style/typography_overline"/>

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:baselineAligned="false"
                    android:weightSum="3">

                <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:baselineAligned="false">

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="14"
                            android:textColor="@color/colorAccent"
                            style="@style/typography_h2"/>

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="@string/App_Lifestyle_Task_Goal_Title"
                            style="@style/typography_caption"/>

                </LinearLayout>


                <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:baselineAligned="false">

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="2"
                            android:textColor="@color/colorAccent"
                            style="@style/typography_h2"/>

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="@string/App_Lifestyle_Task_ToDo_Title"
                            style="@style/typography_caption"/>

                </LinearLayout>

                <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:baselineAligned="false">

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="24"
                            android:textColor="@color/colorAccent"
                            style="@style/typography_h2"/>

                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:text="@string/App_Lifestyle_Task_ToBuy_Title"
                            style="@style/typography_caption"/>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

include_forfragmentdashboard_section_tobuy.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="@string/App_Lifestyle_Task_ToBuy_Title"
        style="@style/Centered_Subtitle_Overline_Default"/>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        android:weightSum="2"
        android:baselineAligned="false">

    <com.google.android.material.card.MaterialCardView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:cardBackgroundColor="@color/cardView_background_default"
            app:cardElevation="@dimen/Elevation_CardView_Simple">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginBottom="@dimen/Margin_CardView_Simple"
                android:orientation="vertical"
                android:baselineAligned="false">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="4"
                    android:textColor="@color/colorAccent"
                    style="@style/typography_h2"/>

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="All Time"
                    style="@style/typography_caption"/>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>


    <com.google.android.material.card.MaterialCardView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:cardBackgroundColor="@color/cardView_background_default"
            app:cardElevation="@dimen/Elevation_CardView_Simple">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginBottom="@dimen/Margin_CardView_Simple"
                android:orientation="vertical"
                android:baselineAligned="false">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="56"
                    android:textColor="@color/colorAccent"
                    style="@style/typography_h2"/>

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Completed"
                    style="@style/typography_caption"/>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

</LinearLayout>


<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="Priorities"
        android:textColor="@color/colorTextAlternateLight"
        style="@style/typography_caption"/>


<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:weightSum="2">

    <ProgressBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:max="@integer/ProgressBar_Default_MaxProgress"
            android:progress="56"
            android:progressTint="@color/item_priority_1"
            android:scaleY="@integer/ProgressBar_Default_ScaleY"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

    <ProgressBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:max="@integer/ProgressBar_Default_MaxProgress"
            android:progress="56"
            android:progressTint="@color/item_priority_2"
            android:scaleY="@integer/ProgressBar_Default_ScaleY"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

</LinearLayout>


<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:weightSum="2">

    <ProgressBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:max="@integer/ProgressBar_Default_MaxProgress"
            android:progress="56"
            android:progressTint="@color/item_priority_3"
            android:scaleY="@integer/ProgressBar_Default_ScaleY"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

    <ProgressBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:max="@integer/ProgressBar_Default_MaxProgress"
            android:progress="56"
            android:progressTint="@color/item_priority_4"
            android:scaleY="@integer/ProgressBar_Default_ScaleY"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

</LinearLayout>


</LinearLayout>

输出是:

用户界面-1

用户界面-2

有人可以通过我可以遵循的一些代码或一些最佳 UI 实践来帮助我改进 UI 以完全优化我的 UI 吗?您可以在 xml 或后面的代码中输入一些代码以减少透支吗?请帮我。

标签: androidperformance

解决方案


推荐阅读