Android:改善UI性能

时间:2019-07-08 12:16:47

标签: android performance

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

我的UI代码如下:

<?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>

输出为:

UI-1

UI-2

有人可以帮助我通过一些代码或一些最佳UI惯例来改进UI,我可以遵循这些准则或一些最佳UI惯例来完全优化UI?您是否可以在xml中输入一些代码或在其后输入代码以减少透支? 请帮助我。

0 个答案:

没有答案