Android性能优化调度延迟

时间:2019-05-02 18:56:43

标签: android performance android-viewpager android-textinputlayout

我在一个Activity中有一个ViewPager,每个片段包含4,5 TextInputLayouts。当NavigationMenu中的活动打开时,它会在打开活动之前造成小的延迟。我拿了systrace来了解发生了什么。大多数情况下,我会遇到一些问题,报告Scheduling Delays。虽然我已经研究了调度延迟,但是我似乎无法弄清楚自己在做什么,因为我只是在夸大布局,并且没有在UI线程上做任何背景工作或其他繁重的工作 >。以下是systrace

中报告的调度延迟
Running 
85.094 ms
Not scheduled, but runnable 
1.949 ms
Blocking I/O delay  
0.259 ms
Sleeping    
13.885 ms
Frame   
Description 
Work to produce this frame was descheduled for several milliseconds, contributing to jank. Ensure that code on the UI thread doesn't block on work being done on other threads, and that background threads (doing e.g. network or bitmap loading) are running at android.os.Process#THREAD_PRIORITY_BACKGROUND or lower so they are less likely to interrupt the UI thread. These background threads should show up with a priority number of 130 or higher in the scheduling section under the Kernel process.

cpu profiler

由于Inefficient View alpha usagesystrace也在HeiglightView中报告了警报,这是我在布局视图继承中找不到的,也没有在任何地方使用它。

view name   
"HighlightView"
width   
990
height  
55
HighlightView alpha caused saveLayer 990x55 took 0.01ms
Description 
Setting an alpha between 0 and 1 has significant performance costs, if one of the fast alpha paths is not used.

布局文件

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


    <androidx.core.widget.NestedScrollView android:layout_width="match_parent"
                                           android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/attach_image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textAllCaps="true"
                    android:drawableEnd="@drawable/ic_cloud_upload"
                    android:drawableRight="@drawable/ic_cloud_upload"
                    android:gravity="center_vertical"
                    android:text="@string/attach_image_to_upload" app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginTop="16dp" android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    android:layout_marginRight="16dp" android:layout_marginEnd="16dp"/>

            <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:id="@+id/creator_name"
                    app:validation_key="@string/validator_non_empty"
                    app:hintAnimationEnabled="false"
                    app:layout_constraintTop_toBottomOf="@+id/attach_image"
                    android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp" android:layout_marginTop="16dp">

                <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="text"
                        android:maxLength="400"
                        android:maxLines="1"
                        android:hint="@string/creator_name"/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:id="@+id/survey_name"
                    app:validation_key="@string/validator_non_empty"
                    app:hintAnimationEnabled="false"
                    app:layout_constraintTop_toBottomOf="@+id/creator_name"
                    android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp" android:layout_marginTop="16dp">

                <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="text"
                        android:maxLength="400"
                        android:maxLines="1"
                        android:hint="@string/survey_name"/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:id="@+id/order_number"
                    app:hintAnimationEnabled="false"
                    app:validation_key="@string/validator_non_empty"
                    app:layout_constraintTop_toBottomOf="@+id/survey_name"
                    android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp" android:layout_marginTop="16dp">

                <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:maxLength="400"
                        android:inputType="number"
                        android:maxLines="1"
                        android:hint="@string/order_number"/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:id="@+id/prize"
                    app:validation_key="@string/validator_non_empty"
                    app:hintAnimationEnabled="false"
                    app:layout_constraintTop_toBottomOf="@+id/order_number"
                    android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp" android:layout_marginTop="16dp">

                <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="text"
                        android:maxLength="400"
                        android:maxLines="1"
                        android:hint="@string/prize"/>
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:id="@+id/description"
                    app:validation_key="@string/validator_non_empty"
                    app:hintAnimationEnabled="false"
                    app:layout_constraintTop_toBottomOf="@+id/prize"
                    android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp" android:layout_marginTop="16dp" android:layout_marginBottom="16dp"
                    app:layout_constraintBottom_toBottomOf="parent">

                <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="text"
                        android:gravity="start"
                        android:maxLength="400"
                        android:maxLines="1"
                        android:imeOptions="actionDone"
                        android:hint="@string/description"/>
            </com.google.android.material.textfield.TextInputLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</layout>

查看层次结构

view hierarchy

0 个答案:

没有答案