|
@@ -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()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|