滚动文字滞后

时间:2018-01-09 14:36:56

标签: android xml android-layout android-studio

我在移动应用中使用了一些scrolling textViews,但我最近发现text启动scrolling时存在一些延迟。有关为什么会发生这种情况的任何想法?我有两个不同的fragments使用相同的代码,但scrolling textViews在另一个上没有“滞后”。我想弄清楚问题是什么,但我找不到它。欢迎任何帮助!

layout.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<!--
<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" /> -->

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<!-- passive skill desc
<TextView
    android:id="@+id/passiveSkillDesc"
    android:layout_width="395dp"
    android:layout_height="33dp"
    android:fontFamily="monospace"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="13sp"
    android:textStyle="italic"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="602dp" />
    -->

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="225dp"
    android:background="@drawable/card_details_closed">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="37dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="78dp"
            android:layout_toEndOf="@+id/HP"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="46dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

    </RelativeLayout>

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackTitle"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="14dp"
        android:layout_marginEnd="35dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackDesc"
        android:layout_alignStart="@+id/superAttackDesc"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/superAttackDesc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_alignEnd="@+id/leaderSkillDesc"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="73dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</RelativeLayout>

<!-- passive skill title
<TextView
    android:id="@+id/passiveSkillTitle"
    android:layout_width="189dp"
    android:layout_height="30dp"
    android:fontFamily="monospace"
    android:textAlignment="viewStart"
    android:textColor="@color/cardDetailsTextColor"
    android:textSize="12sp"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="207dp"
    tools:layout_editor_absoluteY="543dp" />
    -->

</LinearLayout>

更新 当我将手机分辨率转换为4K(s8)时,这种滞后实际上也发生在第二个片段中!

2 个答案:

答案 0 :(得分:1)

您可能遇到

问题
android:layout_weight="1"

设置权重对性能不利,如here

所述
  

嵌套权重对性能不利,因为:

     
    

布局权重需要测量窗口小部件两次。当具有非零权重的LinearLayout嵌套在另一个内部时     LinearLayout具有非零权重,然后是测量次数     以指数方式增长

  

然而,自那时起事情发生了变化,谷歌引入了新的布局:ConstraintLayout。我强烈建议您使用它:ConstraintLayout

答案 1 :(得分:0)

您必须通过将字符串转换为html-string

来有效地加载大文本
    private void someMethod(){
yourTextView.setText(Html.fromHTML(escape(someText)));
yourTextView.setMovementMethod(new ScrollingMovementMethod());}

    public static String escape(String s) {
        StringBuilder builder = new StringBuilder();
        boolean previousWasASpace = false;
        for( char c : s.toCharArray() ) {
            if( c == ' ' ) {
                if( previousWasASpace ) {
                    builder.append("&nbsp;");
                    previousWasASpace = false;
                    continue;
                }
                previousWasASpace = true;
            } else {
                previousWasASpace = false;
            }
            switch(c) {
                case '<': builder.append("&lt;"); break;
                case '>': builder.append("&gt;"); break;
                case '&': builder.append("&amp;"); break;
                case '"': builder.append("&quot;"); break;
                case '\n': builder.append("<br>"); break;
                // We need Tab support here, because we print StackTraces as HTML
                case '\t': builder.append("&nbsp; &nbsp; &nbsp;"); break;  
                default:
                    if( c < 128 ) {
                        builder.append(c);
                    } else {
                        builder.append("&#").append((int)c).append(";");
                    }    
            }
        }
        return builder.toString();
    }

Seconly,必须让你的RelativeLayouts成为an expensive parent

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<!--
<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" /> -->

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<!-- passive skill desc
<TextView
    android:id="@+id/passiveSkillDesc"
    android:layout_width="395dp"
    android:layout_height="33dp"
    android:fontFamily="monospace"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="13sp"
    android:textStyle="italic"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="602dp" />
    -->

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="225dp"
    android:background="@drawable/card_details_closed">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:orientation="vertical"> 
        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="37dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="78dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

 </LinearLayout>
        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginEnd="46dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

    </FrameLayout>
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackTitle"
        android:layout_marginBottom="14dp"
        android:layout_marginEnd="35dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackDesc"
        android:layout_alignStart="@+id/superAttackDesc"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_marginBottom="73dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

 </LinearLayout>
</FrameLayout>

<!-- passive skill title
<TextView
    android:id="@+id/passiveSkillTitle"
    android:layout_width="189dp"
    android:layout_height="30dp"
    android:fontFamily="monospace"
    android:textAlignment="viewStart"
    android:textColor="@color/cardDetailsTextColor"
    android:textSize="12sp"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="207dp"
    tools:layout_editor_absoluteY="543dp" />
    -->

</LinearLayout>

最后,如果您要记录文本,请删除所有要记录的电话