Browse Source

优化录音文案自动滚动问题

zk 1 year ago
parent
commit
02de045b0a

+ 16 - 15
app/src/main/java/com/atmob/voiceai/widget/AutoScrollTextView.java

@@ -2,6 +2,8 @@ package com.atmob.voiceai.widget;
 
 
 import android.annotation.SuppressLint;
 import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
 import android.text.method.ScrollingMovementMethod;
 import android.text.method.ScrollingMovementMethod;
 import android.util.AttributeSet;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.MotionEvent;
@@ -11,6 +13,9 @@ import androidx.annotation.Nullable;
 
 
 public class AutoScrollTextView extends androidx.appcompat.widget.AppCompatTextView {
 public class AutoScrollTextView extends androidx.appcompat.widget.AppCompatTextView {
 
 
+    private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
+    private Runnable autoScrollRunnable;
+
 
 
     public AutoScrollTextView(@NonNull Context context) {
     public AutoScrollTextView(@NonNull Context context) {
         this(context, null);
         this(context, null);
@@ -27,6 +32,14 @@ public class AutoScrollTextView extends androidx.appcompat.widget.AppCompatTextV
 
 
     private void init() {
     private void init() {
         setMovementMethod(ScrollingMovementMethod.getInstance());
         setMovementMethod(ScrollingMovementMethod.getInstance());
+        autoScrollRunnable = () -> {
+            int scrollY = getScrollY();
+            if (scrollY < getLineHeight() * getLineCount() - getHeight()) {
+                scrollY += 1;
+                scrollTo(0, scrollY);
+            }
+            MAIN_HANDLER.postDelayed(autoScrollRunnable, 50);
+        };
     }
     }
 
 
 
 
@@ -39,29 +52,17 @@ public class AutoScrollTextView extends androidx.appcompat.widget.AppCompatTextV
 
 
     public void startScroll() {
     public void startScroll() {
         scrollTo(0, 0);
         scrollTo(0, 0);
-        postScroll();
+        MAIN_HANDLER.postDelayed(autoScrollRunnable, 3000);
     }
     }
 
 
     public void stopScroll() {
     public void stopScroll() {
-        getHandler().removeCallbacksAndMessages(null);
+        MAIN_HANDLER.removeCallbacksAndMessages(null);
     }
     }
 
 
-    private void postScroll() {
-        post(() -> {
-            int scrollY = getScrollY();
-            scrollY += 1;
-            scrollTo(0, scrollY);
-            if (scrollY >= getLineHeight() * getLineCount() - getHeight()) {
-//                scrollTo(0, 0);
-                stopScroll();
-            }
-            postDelayed(this::postScroll, 50);
-        });
-    }
 
 
     @Override
     @Override
     protected void onDetachedFromWindow() {
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
         super.onDetachedFromWindow();
-        removeCallbacks(this::postScroll);
+        stopScroll();
     }
     }
 }
 }

+ 4 - 4
app/src/main/res/layout/item_voice_ai_list.xml

@@ -71,15 +71,15 @@
             android:layout_width="0dp"
             android:layout_width="0dp"
             android:layout_height="0dp"
             android:layout_height="0dp"
             android:background="@color/transparent"
             android:background="@color/transparent"
-            app:lottie_autoPlay="true"
-            app:lottie_loop="true"
-            app:lottie_fileName="anim/voice_ai_example_loading.json"
             app:layout_constraintBottom_toBottomOf="@+id/v_voice_playing"
             app:layout_constraintBottom_toBottomOf="@+id/v_voice_playing"
             app:layout_constraintDimensionRatio="1:1"
             app:layout_constraintDimensionRatio="1:1"
             app:layout_constraintEnd_toEndOf="@+id/v_voice_playing"
             app:layout_constraintEnd_toEndOf="@+id/v_voice_playing"
             app:layout_constraintStart_toStartOf="@+id/v_voice_playing"
             app:layout_constraintStart_toStartOf="@+id/v_voice_playing"
             app:layout_constraintTop_toTopOf="@+id/v_voice_playing"
             app:layout_constraintTop_toTopOf="@+id/v_voice_playing"
-            app:layout_constraintWidth_percent="0.5128205128205128" />
+            app:layout_constraintWidth_percent="0.5128205128205128"
+            app:lottie_autoPlay="true"
+            app:lottie_fileName="anim/voice_ai_example_loading.json"
+            app:lottie_loop="true" />
 
 
         <View
         <View
             isGone="@{!bean.check}"
             isGone="@{!bean.check}"