gpt4 book ai didi

android - ObjectAnimator.ofFloat 不能直接在kotlin中取Int作为参数

转载 作者:IT老高 更新时间:2023-10-28 13:35:31 25 4
gpt4 key购买 nike

我正在开发一个 kotlin 项目,我尝试将 java 中的方法转换为 kotlin。我现在收到此错误

不能使用提供的参数调用以下函数。

它发生在 ObjectAnimator.ofFloat()

代码如下

代码

fun animate(holder: RecyclerView.ViewHolder, goesDown: Boolean) {

val animat = AnimatorSet()

val objectY = ObjectAnimator.ofFloat(holder.itemView, "translationY", if (goesDown) 200 else -200, 0)
objectY.setDuration(Kons.Duration.toLong())

val objectX = ObjectAnimator.ofFloat(holder.itemView, "translationX", -50, 50, -30, 30, -20, 20, -5, 5, 0)
objectX.setDuration(Kons.Duration.toLong())

animat.playTogether(objectX, objectY)
animat.start()
}

最佳答案

看起来,该错误是由 Kotlin 与 Java 不同,does not promote integer literals to floating point types 引起的。 .例如,您必须将它们编写为 200f,并修复这两行:

val objectY = ObjectAnimator.ofFloat(holder.itemView, View.TRANSLATION_Y, if (goesDown) 200f else -200f, 0f)

val objectX = ObjectAnimator.ofFloat(holder.itemView, View.TRANSLATION_X, -50f, 50f, -30f, 30f, -20f, 20f, -5f, 5f, 0f)

关于android - ObjectAnimator.ofFloat 不能直接在kotlin中取Int作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44850128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com