如何在 Kotlin 中为 ObjectAnimator 添加监听器?

3

我做了类似于这样的事情

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})

但是它没有起作用。

什么出了问题?你是否收到错误信息或动画没有正常工作? - Giorgos Neokleous
1个回答

2

虽然您的问题不太清楚,因为您没有说明哪里出了问题。我的猜测是您的监听器没有用。

您先启动了动画,然后再添加它,这显然永远不会被调用。

请按以下方式更改:

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

既然你正在使用 apply,那么 addListener(...) 应该是有效的,而不是 animator.addListener(...),对吗? - Taslim Oseni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接