Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectAnimator.ofFloat can't take Int as parameters directly in kotlin

Tags:

android

kotlin

I am working on a kotlin Project and i try to convert a method in java to kotlin. I am now getting this error

None of the following functions can be called with the arguments supplied.

it occurs on the ObjectAnimator.ofFloat()

The Code is below

Code

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()
}
like image 922
Tandoh Anthony Nwi-Ackah Avatar asked Jun 30 '17 15:06

Tandoh Anthony Nwi-Ackah


1 Answers

Seemingly, the error is caused by the fact that Kotlin, unlike Java, does not promote integer literals to floating point types. You have to write them as, for example, 200f, and fix these two lines:

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)
like image 143
hotkey Avatar answered Oct 13 '22 04:10

hotkey