使用 Lambda 将监听器添加到 ObjectAnimator
Add Listener to ObjectAnimator using Lambda
我已经设置了这个 objectAnimator,我需要添加一个侦听器:
val animator = ObjectAnimator.ofFloat(star, View.TRANSLATION_X, 200f)
.apply {
repeatCount = 1
repeatMode = ObjectAnimator.REVERSE
start()
}
我知道如何使用匿名监听器,如下所示:
animator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
//callback 1 - do something
}
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
//callback 2 - do something
}
我现在想尝试使用 lambda 版本,但我对如何调用侦听器的回调感到困惑:我的代码如下所示
animator.addListener {
onStart(it) {
//get errors with this code
}
请帮忙。
我认为你可以那样做。您可以通过它来选择动画对象。
animator.addListener(
onEnd = { animator: Animator -> animator.start() },
onStart = {},
onCancel = {},
onRepeat = {})
Kotlin 示例:
val sizeAnimation = ValueAnimator.ofFloat(lineWidth * 2)
sizeAnimation
.apply {
addUpdateListener {
annimation.strokeWidth = it.animatedValue as Float
myView.postInvalidateOnAnimation()
}
repeatCount = 5
repeatMode = ValueAnimator.REVERSE
interpolator = LinearInterpolator()
duration = 1000L
start()
}.doOnEnd {
// What to do in the end? // sizeAnimation.start()
}
我已经设置了这个 objectAnimator,我需要添加一个侦听器:
val animator = ObjectAnimator.ofFloat(star, View.TRANSLATION_X, 200f)
.apply {
repeatCount = 1
repeatMode = ObjectAnimator.REVERSE
start()
}
我知道如何使用匿名监听器,如下所示:
animator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
//callback 1 - do something
}
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
//callback 2 - do something
}
我现在想尝试使用 lambda 版本,但我对如何调用侦听器的回调感到困惑:我的代码如下所示
animator.addListener {
onStart(it) {
//get errors with this code
}
请帮忙。
我认为你可以那样做。您可以通过它来选择动画对象。
animator.addListener(
onEnd = { animator: Animator -> animator.start() },
onStart = {},
onCancel = {},
onRepeat = {})
Kotlin 示例:
val sizeAnimation = ValueAnimator.ofFloat(lineWidth * 2)
sizeAnimation
.apply {
addUpdateListener {
annimation.strokeWidth = it.animatedValue as Float
myView.postInvalidateOnAnimation()
}
repeatCount = 5
repeatMode = ValueAnimator.REVERSE
interpolator = LinearInterpolator()
duration = 1000L
start()
}.doOnEnd {
// What to do in the end? // sizeAnimation.start()
}