Browse Source

[feat]键盘插件,封装Lottie工具类

hezihao 7 months ago
parent
commit
7812fcc5c1

+ 5 - 18
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/item/AiGenerateLoadingViewBinder.kt

@@ -5,9 +5,9 @@ import android.view.View
 import android.view.ViewGroup
 import androidx.recyclerview.widget.RecyclerView
 import com.airbnb.lottie.LottieAnimationView
-import com.airbnb.lottie.LottieDrawable
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.model.AiGenerateLoadingModel
+import com.atmob.keyboard_android.util.LottieAnimationUtil
 import me.drakeet.multitype.ItemViewBinder
 
 /**
@@ -32,23 +32,10 @@ class AiGenerateLoadingViewBinder :
         holder: InnerViewBinder,
         item: AiGenerateLoadingModel
     ) {
-        playLottieAnimation(holder.vLoadingLottieView)
-    }
-
-    /**
-     * 播放Lottie动画
-     */
-    private fun playLottieAnimation(animationView: LottieAnimationView) {
-        animationView.setAnimation("lottie/anim_loading.json")
-        animationView.repeatCount = LottieDrawable.INFINITE
-        animationView.playAnimation()
-    }
-
-    /**
-     * 停止Lottie动画
-     */
-    private fun stopLottieAnimation(animationView: LottieAnimationView) {
-        animationView.cancelAnimation()
+        LottieAnimationUtil.playLottieAnimation(
+            holder.vLoadingLottieView,
+            "lottie/anim_loading.json"
+        )
     }
 
     inner class InnerViewBinder(itemView: View) : RecyclerView.ViewHolder(itemView) {

+ 3 - 19
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/item/AiKeyboardKeyViewBinder.kt

@@ -6,12 +6,12 @@ import android.view.ViewGroup
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.airbnb.lottie.LottieAnimationView
-import com.airbnb.lottie.LottieDrawable
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ext.setGone
 import com.atmob.keyboard_android.ext.setVisible
 import com.atmob.keyboard_android.model.AiKeyboardKeyModel
+import com.atmob.keyboard_android.util.LottieAnimationUtil
 import me.drakeet.multitype.ItemViewBinder
 
 /**
@@ -36,7 +36,7 @@ class AiKeyboardKeyViewBinder(
         if (item.isLoading) {
             holder.vLoveFlyLoadingLottieView.setVisible()
             holder.vKeyText.setGone()
-            playLottieAnimation(holder.vLoveFlyLoadingLottieView)
+            LottieAnimationUtil.playLottieAnimation(holder.vLoveFlyLoadingLottieView, "lottie/anim_keyboard_love_fly.json")
         } else {
             // 隐藏Loading,显示文字
             holder.vLoveFlyLoadingLottieView.setGone()
@@ -45,29 +45,13 @@ class AiKeyboardKeyViewBinder(
             holder.vKeyText.apply {
                 text = item.text
             }
-            stopLottieAnimation(holder.vLoveFlyLoadingLottieView)
+            LottieAnimationUtil.stopLottieAnimation(holder.vLoveFlyLoadingLottieView)
         }
         holder.itemView.click {
             onItemClick.invoke(item)
         }
     }
 
-    /**
-     * 播放Lottie动画
-     */
-    private fun playLottieAnimation(animationView: LottieAnimationView) {
-        animationView.setAnimation("lottie/anim_keyboard_love_fly.json")
-        animationView.repeatCount = LottieDrawable.INFINITE
-        animationView.playAnimation()
-    }
-
-    /**
-     * 停止Lottie动画
-     */
-    private fun stopLottieAnimation(animationView: LottieAnimationView) {
-        animationView.cancelAnimation()
-    }
-
     inner class InnerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         val vKeyText: TextView = itemView.findViewById(R.id.key_text)
         val vLoveFlyLoadingLottieView: LottieAnimationView =

+ 27 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/LottieAnimationUtil.kt

@@ -0,0 +1,27 @@
+package com.atmob.keyboard_android.util
+
+import com.airbnb.lottie.LottieAnimationView
+import com.airbnb.lottie.LottieDrawable
+
+/**
+ * Lottie动画工具类
+ */
+class LottieAnimationUtil private constructor() {
+    companion object {
+        /**
+         * 播放Lottie动画
+         */
+        fun playLottieAnimation(animationView: LottieAnimationView, json: String) {
+            animationView.setAnimation(json)
+            animationView.repeatCount = LottieDrawable.INFINITE
+            animationView.playAnimation()
+        }
+
+        /**
+         * 停止Lottie动画
+         */
+        fun stopLottieAnimation(animationView: LottieAnimationView) {
+            animationView.cancelAnimation()
+        }
+    }
+}