问题描述
我正在尝试使用Activity
为ObjectAnimator
中的3个图标设置动画。
动画本身可以正常工作,但是当我通过锁定和解锁屏幕进行测试时,通常会在第二次锁定和解锁屏幕之后冻结/卡住动画。一旦卡住,锁定和解锁就不会释放/恢复动画。
我尝试分别取消并启动Animators
和onPause()
中的onResume()
,但似乎不起作用。还尝试了其他有关堆栈溢出的建议,但找不到适合此问题的解决方案。
负责动画的类:
class MyAnimator(views: List<View>) {
private var animators = listof<Animator>()
init {
animators = views.map { createAnimator(it) }
}
fun startAnimations() {
animators.forEach {
it.start()
}
}
fun cancelAnimations() {
animators.forEach {
it.cancel()
}
}
private fun createAnimator(view: View): Animator {
val jumpAnimation = ObjectAnimator.ofFloat(view,"y",40f,360f).apply {
repeatCount = INFINITE
repeatMode = ValueAnimator.REVERSE
}
val rotateAnimation = ObjectAnimator.ofFloat(view,"rotation",360f).apply {
repeatCount = INFINITE
repeatMode = ValueAnimator.REVERSE
}
return AnimatorSet().apply {
duration = Random.nextLong(1200,1800)
playTogether(jumpAnimation,rotateAnimation)
}
}
}
活动:
class MyActivity : AppCompatActivity() {
private lateinit var animator: MyAnimator
private var icons = listof<ImageView>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_layout)
gameIcons = listof(firstIconIV,secondIconIV,thirdIconIV)
animator = MyAnimator(gameIcons)
}
override fun onPause() {
super.onPause()
animator.cancelAnimations()
}
override fun onResume() {
super.onResume()
animator.startAnimations()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)