Created
June 28, 2023 12:50
-
-
Save Rohit-554/5763e3397a42cf4df7a855c24fb5f0c4 to your computer and use it in GitHub Desktop.
This is for implementing the lucky wheel and getting the desired answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.apphub.ea3.ui.features.earnCash | |
| import android.annotation.SuppressLint | |
| import android.os.Bundle | |
| import android.util.Log | |
| import androidx.fragment.app.Fragment | |
| import android.view.LayoutInflater | |
| import android.view.MotionEvent | |
| import android.view.View | |
| import android.view.View.OnTouchListener | |
| import android.view.ViewGroup | |
| import android.view.animation.Animation | |
| import android.view.animation.DecelerateInterpolator | |
| import android.view.animation.RotateAnimation | |
| import android.widget.ImageView | |
| import android.widget.TextView | |
| import android.widget.Toast | |
| import com.apphub.ea3.MainActivity | |
| import com.apphub.ea3.databinding.FragmentSpinBinding | |
| import kotlinx.coroutines.CoroutineScope | |
| import kotlinx.coroutines.Dispatchers | |
| import kotlinx.coroutines.delay | |
| import kotlinx.coroutines.launch | |
| import kotlin.math.floor | |
| import kotlin.random.Random | |
| @SuppressLint("ClickableViewAccessibility") | |
| class SpinFragment : Fragment(), Animation.AnimationListener { | |
| private lateinit var binding: FragmentSpinBinding | |
| private var prizes: Array<String>? = null | |
| private var sectorDegrees:Array<Int>?= null | |
| private var isAnimationRunning = false | |
| private var degree = 0 | |
| override fun onCreateView( | |
| inflater: LayoutInflater, container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View { | |
| binding = FragmentSpinBinding.inflate(inflater, container, false) | |
| (activity as MainActivity).hideBottomNav() | |
| prizes = arrayOf("0", "12", "5", "10", "1", "2", "15", "50", "9", "17", "5", "100") | |
| getDegreeForSectors() | |
| binding.ivSpinBoard.setOnClickListener { | |
| if(!isAnimationRunning){ | |
| spin() | |
| isAnimationRunning = true | |
| } | |
| } | |
| return binding.root | |
| } | |
| private fun getDegreeForSectors(){ | |
| val numOfPrizes = prizes?.size ?: 0 | |
| sectorDegrees = Array(numOfPrizes) { 0 } | |
| val sectorDegree = 360 / numOfPrizes | |
| for (i in 0 until numOfPrizes) { | |
| sectorDegrees?.set(i, (i + 1) * sectorDegree) | |
| } | |
| Log.d("TAG", "getDegreeForSectors: ${sectorDegrees?.size}, $sectorDegree") | |
| } | |
| private fun spin(){ | |
| degree = Random.nextInt(prizes?.size?.minus(1) ?: 0) | |
| Log.d("TAG", "spin: ${prizes?.size}, ${sectorDegrees?.size}") | |
| val rotateAnimation = RotateAnimation( | |
| 0f, | |
| (360*prizes?.size!!) + sectorDegrees?.get(degree)!!.toFloat(), | |
| Animation.RELATIVE_TO_SELF, | |
| 0.5f, | |
| Animation.RELATIVE_TO_SELF, | |
| 0.5f | |
| ) | |
| rotateAnimation.duration = 3600 | |
| rotateAnimation.fillAfter = true | |
| rotateAnimation.interpolator = DecelerateInterpolator() | |
| rotateAnimation.setAnimationListener(this) | |
| binding.ivSpinBoard.startAnimation(rotateAnimation) | |
| } | |
| override fun onAnimationStart(p0: Animation?) { | |
| } | |
| override fun onAnimationEnd(p0: Animation?) { | |
| Toast.makeText(requireContext(), "got ${prizes?.get(prizes?.size!!-(degree+1))}", Toast.LENGTH_SHORT).show() | |
| isAnimationRunning = false | |
| } | |
| override fun onAnimationRepeat(p0: Animation?) { | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment