旋转动画期间更新属性 [英] Update Attributes during Rotation Animation
问题描述
我正在Kotlin中制作一个旋转动画,我想在每个动画之后设置一个net rotationStart和rotationEnd,其中最初应将rotationStart值设置为0,然后在每次旋转后采用rotationEnd值的值
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)
像这样设置轮播:
var rotateAnimation = RotateAnimation(
rotStart, rotEnd,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 4000
rotateAnimation.repeatCount = 4
与听众:
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)
}
这里的问题是,值rotStart和rotEnd在每个周期后都不会更新.我忘记实现了onUpdateListener吗?
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?
推荐答案
好吧,到目前为止,我发现的唯一方法就是这里(以防万一没有人回答这个问题,这是我的解决方案)
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
此后,您必须在每个onAnimationEnd上进行设置: imageview.startAnimation(ra#NUMBER#). 这是一种解决方法,而不是干净的解决方案:-)
after this you have to set on each onAnimationEnd: imageview.startAnimation(ra#NUMBER#). It's a workaround and not a clean solution :-)
这篇关于旋转动画期间更新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!