|
@@ -1,34 +1,27 @@
|
|
|
package com.swago.baseswago.cusview
|
|
|
|
|
|
-import android.animation.ObjectAnimator
|
|
|
-import android.annotation.SuppressLint
|
|
|
+import android.animation.Animator
|
|
|
+import android.animation.ValueAnimator
|
|
|
import android.content.Context
|
|
|
-import android.os.Handler
|
|
|
-import android.os.Looper
|
|
|
import android.util.AttributeSet
|
|
|
import android.view.LayoutInflater
|
|
|
-import android.view.View
|
|
|
+import android.view.animation.DecelerateInterpolator
|
|
|
import android.widget.FrameLayout
|
|
|
-import com.bumptech.glide.Glide
|
|
|
import com.swago.baseswago.R
|
|
|
import com.swago.baseswago.databinding.ItemLotteryBinding
|
|
|
import com.swago.baseswago.databinding.ViewLotteryBinding
|
|
|
import com.swago.baseswago.model.mine.LotteryListModel
|
|
|
-import com.swago.loadNoPlaceUrlSrc
|
|
|
-import com.swago.loadUrl
|
|
|
+import com.swago.loadNoPlaceUrl
|
|
|
|
|
|
class LotteryView : FrameLayout {
|
|
|
private var binding:ViewLotteryBinding? = null
|
|
|
|
|
|
- private var items = mutableListOf<ItemLotteryBinding>() // 保存16个格子
|
|
|
- private var lotteryData = mutableListOf<LotteryListModel>() // 保存16个格子
|
|
|
- private var animatorCount = 0 //当前动画刷新次数
|
|
|
- private var rotationSpeed = 50L //动画速度
|
|
|
+ private var isAnimating = false // 判断是否正在执行动画
|
|
|
+ private var totalRounds = 3 // 旋转的圈数
|
|
|
+ private var items = mutableListOf<ItemLotteryBinding>() // 保存12个格子
|
|
|
+ private var lotteryData = mutableListOf<LotteryListModel>() // 保存12个格子
|
|
|
private var mResult : LotteryListModel ?= null
|
|
|
|
|
|
- private var currentIndex = 0 // 当前选中的格子索引
|
|
|
- private var isAnimating = false // 判断是否正在执行动画
|
|
|
- private val handler = Handler(Looper.getMainLooper()) // 用于定时控制的 Handler
|
|
|
var startGetResultFun: (() -> Unit)?= null
|
|
|
var imageLoadSuccessFun: ((success:Boolean) -> Unit)?= null
|
|
|
var showGetResultFun: ((result:LotteryListModel?) -> Unit)?= null
|
|
@@ -61,7 +54,6 @@ class LotteryView : FrameLayout {
|
|
|
}
|
|
|
binding?.ivStart?.setOnClickListener {
|
|
|
startGetResultFun?.invoke()
|
|
|
-// startLottery(null)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -71,84 +63,56 @@ class LotteryView : FrameLayout {
|
|
|
lotteryData.clear()
|
|
|
lotteryData.addAll(list)
|
|
|
items.forEachIndexed { index, view ->
|
|
|
- view.ivIcon.loadNoPlaceUrlSrc(context,list[index].icon){
|
|
|
- imageLoadSuccessFun?.invoke(it)
|
|
|
+ if (list[index].number.startsWith("EXP")){
|
|
|
+ view.ivIcon.setBackgroundResource(R.mipmap.ic_exp)
|
|
|
+ } else {
|
|
|
+ view.ivIcon.loadNoPlaceUrl(context,list[index].icon){
|
|
|
+ imageLoadSuccessFun?.invoke(it)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
view.tvName.text = list[index].number
|
|
|
if (index %2 ==1){
|
|
|
- view.clItem.setBackgroundResource(R.mipmap.background_lottery)
|
|
|
+ view.clItem.setBackgroundResource(R.mipmap.bg_lottery)
|
|
|
} else {
|
|
|
- view.clItem.setBackgroundResource(R.mipmap.background_lottery_purple)
|
|
|
+ view.clItem.setBackgroundResource(R.mipmap.bg_lottery_purple)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 开始旋转动画
|
|
|
- fun startLottery(result : LotteryListModel?) {
|
|
|
+ fun startLotteryAnimation(result : LotteryListModel) {
|
|
|
if (isAnimating) return // 防止重复启动
|
|
|
mResult = result
|
|
|
- currentIndex = 0
|
|
|
- isAnimating = true
|
|
|
- currentIndex = -1
|
|
|
- rotationSpeed = 100L // 初始旋转速度
|
|
|
- animatorCount = 0
|
|
|
-
|
|
|
- handler.post(runnable)
|
|
|
- }
|
|
|
-
|
|
|
- // 定义 Runnable 控制动画循环
|
|
|
- private val runnable = object : Runnable {
|
|
|
- override fun run() {
|
|
|
- // 重置上一个格子的高亮状态
|
|
|
- if (currentIndex >= 0) {
|
|
|
- items[currentIndex].clItem.isSelected = false
|
|
|
- }
|
|
|
-
|
|
|
- // 更新当前格子的索引
|
|
|
- currentIndex = (currentIndex + 1) % items.size
|
|
|
- if (currentIndex == 0){
|
|
|
- animatorCount ++
|
|
|
- }
|
|
|
-
|
|
|
- // 高亮当前格子
|
|
|
- items[currentIndex].clItem.isSelected = true
|
|
|
- playHighlightAnimation(items[currentIndex].clItem)
|
|
|
-
|
|
|
- // 判断是否到达目标格子并减速
|
|
|
- if (isAnimating) {
|
|
|
- // 动态增加延迟时间,模拟逐渐减速
|
|
|
- if (animatorCount == 3){
|
|
|
- rotationSpeed = (rotationSpeed * 1.1).toLong() // 每次增加10%的时间
|
|
|
- }
|
|
|
-
|
|
|
- // 停止动画条件:到达目标格子且速度足够慢
|
|
|
- if (lotteryData[currentIndex].id == mResult?.id && animatorCount > 3) {
|
|
|
- onLotteryFinished()
|
|
|
- } else {
|
|
|
- // 继续下一次高亮
|
|
|
- handler.postDelayed(this, rotationSpeed)
|
|
|
- }
|
|
|
- }
|
|
|
+ isAnimating = false
|
|
|
+ val animator = ValueAnimator.ofInt(0, items.size * totalRounds + result.id )
|
|
|
+ animator.duration = 3000 // 动画总时长 3 秒
|
|
|
+ animator.interpolator = DecelerateInterpolator() // 动画减速效果
|
|
|
+ animator.addUpdateListener { animation ->
|
|
|
+ val position = animation.animatedValue as Int % items.size
|
|
|
+ highlightItem(position)
|
|
|
}
|
|
|
- }
|
|
|
+ animator.addListener(object : Animator.AnimatorListener {
|
|
|
+ override fun onAnimationEnd(animation: Animator) {
|
|
|
+ // 动画结束后的处理,显示中奖结果
|
|
|
+ isAnimating = true
|
|
|
+ showGetResultFun?.invoke(mResult)
|
|
|
+ }
|
|
|
|
|
|
+ override fun onAnimationStart(animation: Animator) {}
|
|
|
+ override fun onAnimationCancel(animation: Animator) {}
|
|
|
+ override fun onAnimationRepeat(animation: Animator) {}
|
|
|
+ })
|
|
|
|
|
|
- // 播放高亮动画(例如背景颜色闪烁)
|
|
|
- private fun playHighlightAnimation(view: View) {
|
|
|
- val animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 1f)
|
|
|
- animator.duration = 500 // 每个格子的高亮持续时间
|
|
|
animator.start()
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // 抽奖结束时的逻辑
|
|
|
- private fun onLotteryFinished() {
|
|
|
- isAnimating = false
|
|
|
- // 处理中奖逻辑,例如显示中奖信息或奖励
|
|
|
- showGetResultFun?.invoke(mResult)
|
|
|
- }
|
|
|
-
|
|
|
- fun onDestroy(){
|
|
|
- handler.removeCallbacksAndMessages(null)
|
|
|
+ // 高亮显示某个格子
|
|
|
+ private fun highlightItem(index: Int) {
|
|
|
+ // 清除所有格子的高亮效果
|
|
|
+ for (i in items.indices) {
|
|
|
+ items[i].clChoose.background = null
|
|
|
+ }
|
|
|
+ // 设置当前格子为高亮
|
|
|
+ items[index].clChoose.setBackgroundResource(R.drawable.shape_ffffff_12)
|
|
|
}
|
|
|
}
|