Skip to content

Instantly share code, notes, and snippets.

@Rohit-554
Created June 28, 2023 12:48
Show Gist options
  • Select an option

  • Save Rohit-554/d89a4609619e29a1ab869900e25d8d65 to your computer and use it in GitHub Desktop.

Select an option

Save Rohit-554/d89a4609619e29a1ab869900e25d8d65 to your computer and use it in GitHub Desktop.
This is for implementing with ontouch listener and although it might not show the currect result
private fun startSpinner() {
isAnimationRunning = true
mSpinRevolution = 3600f
mSpinDuration = 5000
when {
(count in 30..59) -> {
mSpinDuration = 1000
mSpinRevolution = (3600 * 2).toFloat()
}
(count >= 60) -> {
mSpinDuration = 15000
mSpinRevolution = (3600 * 2).toFloat()
}
}
val end = floor(Math.random() * 3600).toInt()
val numOfPrizes = prizes?.size
val degree = 360f / numOfPrizes!!
val shift = 1
val prizeIndex = (shift + end) % numOfPrizes
// Toast.makeText(
// requireContext(), "prize is ${
// prizes?.get(prizeIndex)
// }", Toast.LENGTH_SHORT
// ).show()
val rotateAnimation = RotateAnimation(
0f,
mSpinRevolution + end,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f
)
rotateAnimation.interpolator = DecelerateInterpolator()
rotateAnimation.repeatCount = 0
rotateAnimation.duration = mSpinDuration
rotateAnimation.setAnimationListener(this)
rotateAnimation.fillAfter = true
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?) {
}
private fun powerTouchListener(p0: View?, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
if (!isAnimationRunning) {
flag = true
count = 0
CoroutineScope(Dispatchers.Default).launch {
while (flag) {
count++
if (count == 100) {
delay(100)
count = 0
}
delay(10)
}
}
}
return true
}
MotionEvent.ACTION_UP -> {
if(!isAnimationRunning){
flag = false
startSpinner()
return false
}
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment