首页 > 解决方案 > 如何在 Kotlin 中将侦听器添加到 ObjectAnimator?

问题描述

我做了这样的事情

val animator = ObjectAnimator.ofFloat(view, "translationY", 350f,0f)
    animator.duration = 500
    animator.startDelay=200
    animator.interpolator =AccelerateDecelerateInterpolator()
    animator.start()

现在我正在尝试将侦听器添加到此适配器。我试过这个,

animator.addListener(onStart = {view.visibility=View.VISIBLE})

但不工作。

标签: androidkotlinobjectanimator

解决方案


尽管您的问题不清楚,因为您没有提到什么不起作用。我的猜测是你的听众没用。

您正在启动动画制作器,然后添加它,这当然永远不会被调用。

更改如下:

val animator = ObjectAnimator.ofFloat(view, "translationY", 350f, 0f)
animator.apply {
    duration = 500
    startDelay = 200
    addListener(onStart = {
        view.visibility = View.VISIBLE
    })
    AccelerateDecelerateInterpolator()
    start()
}

推荐阅读