소스 검색

fix: lottery

tongmengxiao 3 달 전
부모
커밋
86e5b4cb43

+ 43 - 79
baseswago/src/main/java/com/swago/baseswago/cusview/LotteryView.kt

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

+ 2 - 7
baseswago/src/main/java/com/swago/baseswago/dialog/TodayTrafficDialog.kt

@@ -36,7 +36,7 @@ class TodayTrafficDialog : BaseStoreDFragment<DialogTodayTrafficBinding>() {
             showGetResultFun = {
                 it?.let { model ->
                     this@TodayTrafficDialog.dismissAllowingStateLoss()
-                    TrafficResultDialog.newInstance(model.number , model.icon).show(parentFragmentManager,"TrafficResultDialog")
+                    TrafficResultDialog.newInstance(model.number , model.icon , model.number).show(parentFragmentManager,"TrafficResultDialog")
                 }
             }
         }
@@ -45,7 +45,7 @@ class TodayTrafficDialog : BaseStoreDFragment<DialogTodayTrafficBinding>() {
             dismissAllowingStateLoss()
         }
         lotteryVm.resultLottery.observe(this){
-            binding?.lotteryView?.startLottery(it)
+            binding?.lotteryView?.startLotteryAnimation(it)
         }
 
         lotteryVm.getLotteryList()
@@ -58,9 +58,4 @@ class TodayTrafficDialog : BaseStoreDFragment<DialogTodayTrafficBinding>() {
     override fun initLiveData() {
 
     }
-
-    override fun onDestroyView() {
-        binding?.lotteryView?.onDestroy()
-        super.onDestroyView()
-    }
 }

+ 10 - 3
baseswago/src/main/java/com/swago/baseswago/dialog/TrafficResultDialog.kt

@@ -3,8 +3,9 @@ package com.swago.baseswago.dialog
 
 import android.os.Bundle
 import android.view.Gravity
+import com.swago.baseswago.R
 import com.swago.baseswago.databinding.DialogResultTrafficBinding
-import com.swago.loadNoPlaceUrlSrc
+import com.swago.loadNoPlaceUrl
 
 class TrafficResultDialog : BaseStoreDFragment<DialogResultTrafficBinding>() {
 
@@ -16,11 +17,12 @@ class TrafficResultDialog : BaseStoreDFragment<DialogResultTrafficBinding>() {
     }
 
     companion object {
-        fun newInstance(name:String,icon:String): TrafficResultDialog {
+        fun newInstance(name:String,icon:String,number:String): TrafficResultDialog {
             val args = Bundle()
             val fragment = TrafficResultDialog()
             args.putString("name",name)
             args.putString("icon",icon)
+            args.putString("number",number)
             fragment.arguments = args
             return fragment
         }
@@ -30,9 +32,14 @@ class TrafficResultDialog : BaseStoreDFragment<DialogResultTrafficBinding>() {
         arguments?.let{
             val name = it.getString("name")
             val icon = it.getString("icon","")
+            val number = it.getString("number","")
             binding?.tvName?.text= name
             activity?.let {activity ->
-                binding?.ivIcon?.loadNoPlaceUrlSrc(activity,icon){}
+                if (number.startsWith("EXP")){
+                    binding?.ivIcon?.setBackgroundResource(R.mipmap.ic_exp)
+                } else {
+                    binding?.ivIcon?.loadNoPlaceUrl(activity,icon){}
+                }
             }
 
         }

+ 5 - 0
baseswago/src/main/res/drawable/shape_ffffff_12.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="#FFFFFF"/>
+    <corners android:radius="12dp"/>
+</shape>

+ 1 - 0
baseswago/src/main/res/layout/dialog_result_traffic.xml

@@ -35,6 +35,7 @@
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:layout_marginTop="14dp"
+            android:background="@mipmap/ic_coin"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintTop_toBottomOf="@+id/tvGet"/>

+ 5 - 4
baseswago/src/main/res/layout/item_lottery.xml

@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/clChoose"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     xmlns:app="http://schemas.android.com/apk/res-auto">
@@ -9,16 +10,16 @@
         android:layout_margin="2dp"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        android:background="@mipmap/background_lottery">
+        android:background="@mipmap/bg_lottery">
         <ImageView
             android:id="@+id/iv_icon"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintTop_toTopOf="parent"
             app:layout_constraintBottom_toBottomOf="parent"
-            android:src="@mipmap/icon_exp"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"/>
+            android:background="@mipmap/ic_coin"
+            android:layout_width="50dp"
+            android:layout_height="50dp"/>
 
         <TextView
             android:id="@+id/tvName"

BIN
baseswago/src/main/res/mipmap-xxhdpi/background_lottery.png


BIN
baseswago/src/main/res/mipmap-xxhdpi/background_lottery_purple.png


BIN
baseswago/src/main/res/mipmap-xxhdpi/bg_lottery.png


BIN
baseswago/src/main/res/mipmap-xxhdpi/bg_lottery_purple.png


BIN
baseswago/src/main/res/mipmap-xxhdpi/ic_coin.png


BIN
baseswago/src/main/res/mipmap-xxhdpi/ic_exp.png


+ 1 - 5
home/src/main/java/com/swago/home/MineFragment.kt

@@ -161,11 +161,7 @@ class MineFragment : BaseXFragment<FragmentMineBinding>() {
 
         binding.ivTraffic.setOnClickListener(object :NoDoubleClickListener(){
             override fun onClick() {
-                UserInfo.getUserInfo()?.let{
-                    if (!it.is_zhuanpan_every_day){
-                        TodayTrafficDialog.newInstance().show(childFragmentManager,"TodayTrafficDialog")
-                    }
-                }
+                TodayTrafficDialog.newInstance().show(childFragmentManager,"TodayTrafficDialog")
             }
         })
 

+ 0 - 1
home/src/main/res/layout/fragment_mine.xml

@@ -482,7 +482,6 @@
         android:src="@mipmap/ic_traffic"
         android:layout_marginEnd="12dp"
         android:layout_marginBottom="12dp"
-        android:visibility="gone"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"/>