问题
I'm working on a rotation animation in Kotlin and I after each Animation I want to set a net rotationStart and rotationEnd where the rotationStart value initially should be set = 0 and afterwards assumes the value of the rotationEnd value after each rotation
val rand = Random()
var rotStart : Float = 0f
var rotEnd : Float = rand.nextFloat(100f)
Setting up the rotation like this:
var rotateAnimation = RotateAnimation(
rotStart, rotEnd,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 4000
rotateAnimation.repeatCount = 4
with the listener:
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
}
override fun onAnimationRepeat(animation: Animation?) {
rotStart = rotEnd
rotEnd = rand.nextFloat(359f)
}
override fun onAnimationEnd(animation: Animation?) {
}
})
img_spinner.startAnimation(rotateAnimation)
}
The problem here is, that the value rotStart and rotEnd doesn't get updated after each cycle. Is there a onUpdateListener I forgot to implement?
回答1:
Well, the only way I found until now is this here (just in case nobody will answer to this thread, here is my solution)
val angles = arrayOf(9,6,9,4)
var rotStart = 0f
var imageSpinner = findViewById<ImageView>(R.id.img_spinner)
val ra1 = RotateAnimation(rotStart,-angles[0].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
ra1.duration = 3200
val ra2 = RotateAnimation(rotStart, -angles[1].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
ra2.duration = 1900
val ra3 = RotateAnimation(rotStart, -angles[2].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
ra3.duration = 5400
val ra4 = RotateAnimation(rotStart, -angles[3].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
ra4.duration = 5400
after this you have to set on each onAnimationEnd: imageview.startAnimation(ra#NUMBER#). It's a workaround and not a clean solution :-)
来源:https://stackoverflow.com/questions/59253778/update-attributes-during-rotation-animation