val anim = swipe.animate()
.rotationBy((-30).toFloat())
.setDuration(1000)
.translationX((-swipe.left).toFloat())
.setInterpolator(AccelerateDecelerateInterpolator())
anim.start()
我需要一个动画完成监听器,我尝试过:
anim.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(p0: Animation?) {
}
override fun onAnimationRepeat(p0: Animation?) {
}
override fun onAnimationEnd(p0: Animation?) {
}
})
但出现此错误
Unresolved reference: setAnimationListener
如何正确地做到这一点?
最佳答案
根本原因
在 ViewPropertyAnimator类中,没有名为 setAnimationListener
的方法。
解决方案1
anim.withEndAction {
// Your code logic goes here.
}
解决方案2
anim.setListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) {}
override fun onAnimationCancel(animation: Animator?) {}
override fun onAnimationStart(animation: Animator?) {}
override fun onAnimationEnd(animation: Animator?) {
// Your code logic goes here.
}
})
注意:如果用户在动画播放时离开屏幕,请记住取消动画。
swipe.animate().cancel()
关于Android Kotlin - 设置动画结束/完成监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65117176/