Explorar el Código

feat: 转盘isAnimating

tongmengxiao hace 2 meses
padre
commit
39e112820b

+ 25 - 10
baseswago/src/main/java/com/swago/baseswago/cusview/LotteryView.kt

@@ -17,10 +17,12 @@ class LotteryView : FrameLayout {
     private var binding:ViewLotteryBinding? = null
 
     private var isAnimating = false // 判断是否正在执行动画
+    private var isCancel = false // 判断动画是否是被取消的
     private var totalRounds = 3  // 旋转的圈数
     private var items = mutableListOf<ItemLotteryBinding>() // 保存12个格子
     private var lotteryData = mutableListOf<LotteryListModel>() // 保存12个格子
     private var mResult : LotteryListModel ?= null
+    private var mValueAnimator:ValueAnimator ?= null
 
     var startGetResultFun: (() -> Unit)?= null
     var imageLoadSuccessFun: ((success:Boolean) -> Unit)?= null
@@ -53,6 +55,7 @@ class LotteryView : FrameLayout {
             items.add(view12)
         }
         binding?.ivStart?.setOnClickListener {
+            if (isAnimating) return@setOnClickListener  // 防止重复启动
             startGetResultFun?.invoke()
         }
     }
@@ -83,27 +86,32 @@ class LotteryView : FrameLayout {
     fun startLotteryAnimation(result : LotteryListModel) {
         if (isAnimating) return  // 防止重复启动
         mResult = result
-        isAnimating = false
-        val animator = ValueAnimator.ofInt(0, items.size * totalRounds + result.id )
-        animator.duration = 3000  // 动画总时长 3 秒
-        animator.interpolator = DecelerateInterpolator()  // 动画减速效果
-        animator.addUpdateListener { animation ->
+        isAnimating = true
+        isCancel = false
+        mValueAnimator = ValueAnimator.ofInt(0, items.size * totalRounds + result.id )
+        mValueAnimator?.duration = 3000  // 动画总时长 3 秒
+        mValueAnimator?.interpolator = DecelerateInterpolator()  // 动画减速效果
+        mValueAnimator?.addUpdateListener { animation ->
             val position = animation.animatedValue as Int % items.size
             highlightItem(position)
         }
-        animator.addListener(object : Animator.AnimatorListener {
+        mValueAnimator?.addListener(object : Animator.AnimatorListener {
             override fun onAnimationEnd(animation: Animator) {
                 // 动画结束后的处理,显示中奖结果
-                isAnimating = true
-                showGetResultFun?.invoke(mResult)
+                if (!isCancel){
+                    isAnimating = false
+                    showGetResultFun?.invoke(mResult)
+                }
             }
 
             override fun onAnimationStart(animation: Animator) {}
-            override fun onAnimationCancel(animation: Animator) {}
+            override fun onAnimationCancel(animation: Animator) {
+                isCancel = true
+            }
             override fun onAnimationRepeat(animation: Animator) {}
         })
 
-        animator.start()
+        mValueAnimator?.start()
     }
 
     // 高亮显示某个格子
@@ -115,4 +123,11 @@ class LotteryView : FrameLayout {
         // 设置当前格子为高亮
         items[index].clChoose.setBackgroundResource(R.drawable.shape_ffffff_12)
     }
+
+    fun onDestroy(){
+        if (isAnimating){
+            mValueAnimator?.cancel()
+        }
+
+    }
 }

+ 5 - 0
baseswago/src/main/java/com/swago/baseswago/dialog/TodayTrafficDialog.kt

@@ -58,4 +58,9 @@ class TodayTrafficDialog : BaseStoreDFragment<DialogTodayTrafficBinding>() {
     override fun initLiveData() {
 
     }
+
+    override fun onDestroyView() {
+        binding?.lotteryView?.onDestroy()
+        super.onDestroyView()
+    }
 }

+ 2 - 1
baseswago/src/main/res/layout/item_lottery.xml

@@ -17,6 +17,7 @@
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintTop_toTopOf="parent"
             app:layout_constraintBottom_toBottomOf="parent"
+            android:layout_marginBottom="@dimen/dp_10"
             android:background="@mipmap/ic_coin"
             android:layout_width="50dp"
             android:layout_height="50dp"/>
@@ -26,7 +27,7 @@
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintBottom_toBottomOf="parent"
-            android:text="×3 Days"
+            tools:text="×3 Days"
             android:textSize="11sp"
             android:textColor="@color/_ffffff"
             android:layout_width="wrap_content"