首页 > 解决方案 > Android性能优化问题(Layout with JustifiedTextViews and CodeViews)

问题描述

我的问题是当我打开一个充满 JustifiedTextView 和 CodeView 的 Activity 时,我的应用程序非常慢。

implementation 'com.github.softwee:codeview-android:1.1.2'
implementation 'com.uncopt:android.justified:1.0'

我使用这些库是为了获得合理的文本并能够使用语法荧光笔。

问题 A:这些 3rd 方库是否有可能使我的应用程序变慢?有时会跳过 100-150 帧!

问题 B:我自己做这些事情是一个好方法吗?所以我自己实现文本对齐等?

问题 C:将字符串存储在数据库 ( SQLite ) 中是否比使用strings.xml更好?

这是一个活动的确切源代码示例:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout 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="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".SortingMainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#858a9f"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:textSize="20dp"
        android:text="What is Bogo Sort?"
        android:textStyle="bold"
        app:fontFamily="@font/roboto_slab" />

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:id="@+id/bogoSortFirstParagraph"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#858a9f"
        android:textSize="18dp"
        app:fontFamily="@font/roboto_slab"
        android:paddingBottom="10dp"/>

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:id="@+id/bogoDeterministic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        app:fontFamily="@font/roboto_slab" />

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:id="@+id/bogoRandom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        app:fontFamily="@font/roboto_slab" />

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:id="@+id/bogoEssence"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        app:fontFamily="@font/roboto_slab" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#858a9f"
        android:paddingTop="10dp"
        android:paddingBottom="20dp"
        android:textSize="20dp"
        android:text="Implementation"
        android:textStyle="bold"
        app:fontFamily="@font/roboto_slab" />

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:id="@+id/bogoFisherYates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        app:fontFamily="@font/roboto_slab" />

    <io.github.kbiakov.codeview.CodeView
        android:id="@+id/shuffleCodeView"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        android:text="@string/bogoTheory1"
        app:fontFamily="@font/roboto_slab" />

    <io.github.kbiakov.codeview.CodeView
        android:id="@+id/swapCodeView"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        android:text="@string/bogoTheory2"
        app:fontFamily="@font/roboto_slab" />

    <io.github.kbiakov.codeview.CodeView
        android:id="@+id/isSortedCodeView"
        android:layout_width="match_parent"
        android:paddingBottom="10dp"
        android:layout_height="wrap_content"/>

    <com.uncopt.android.widget.text.justify.JustifiedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="#858a9f"
        android:textSize="18dp"
        android:text="@string/bogoTheory3"
        app:fontFamily="@font/roboto_slab" />

    <io.github.kbiakov.codeview.CodeView
        android:id="@+id/sortCodeView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

标签: javaandroidxmlandroid-linearlayout

解决方案


如果没有分析,很难确定,但一个好的第一步可能是使用RecyclerView仅渲染屏幕上的部分,而不必使用您当前拥有的LinearLayout/设置来布置整个事物。ScrollView

为了确定,或者作为之后的下一步,您应该尝试从 Android Studio 获取方法跟踪,以查看导致丢帧期间大部分工作的原因。


推荐阅读