I'm trying to randomize the rotation of a circular imageview where the onAnimationRepeat should generate a new rotationEnd value and assume the rotationStart at the position where the last cycle has stopped - this is the code:
var rotStart : Float = 0f
var rotEnd : Float = randomNumberGenerator()
val rand = Random()
var rotateAnimation = RotateAnimation(
rotStart, rotEnd,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 3000
rotateAnimation.repeatCount = 20
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
}
override fun onAnimationRepeat(animation: Animation?) {
rotStart = rotEnd
rotEnd = rand.nextInt(359).toFloat()
rotateAnimation = RotateAnimation(rotStart,rotEnd,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
}
override fun onAnimationEnd(animation: Animation?) {
}
})
img_spinner.startAnimation(rotateAnimation)
}
fun randomNumberGenerator(): Float {
val rangeMin = 0f
val rangeMax = 359f
val r = Random()
return rangeMin + (rangeMax - rangeMin) * r.nextFloat()
}
onAnimationRepeat should start with the value of the rotation-angle the image has experienced on the last cycle. With this code the rotation starts always with the value of: 0f and ends at the fixed value of "rotEnd" which the randomNumberGenerator() generated on the first run, so for example: 0 and 122 is generated on the first run, the image keeps spinning from 0 to 122 and ignores completely my code which I've written onAnimationRepeat.
Aucun commentaire:
Enregistrer un commentaire