在Kotlin中不能使用argb color int值?

时间:2017-06-07 07:49:56

标签: android kotlin

当我想在Kotlin中为textColor的{​​{1}}制作动画时:

TextView

发生此错误:

val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF, 0xFFC953BE)

似乎无法将值Error:(124, 43) None of the following functions can be called with the arguments supplied: public open fun <T : Any!> ofInt(target: TextView!, xProperty: Property<TextView!, Int!>!, yProperty: Property<TextView!, Int!>!, path: Path!): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun <T : Any!> ofInt(target: TextView!, property: Property<TextView!, Int!>!, vararg values: Int): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(target: Any!, propertyName: String!, vararg values: Int): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(target: Any!, xPropertyName: String!, yPropertyName: String!, path: Path!): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(vararg values: Int): ValueAnimator! defined in android.animation.ObjectAnimator 0xFF8363FF强制转换为Kotlin中的0xFFC953BE,但是,它在Java中是正常的:

Int

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:14)

0xFF8363FF(以及0xFFC953BE)是Long,而不是Int

您必须明确地将它们转换为Int

val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF.toInt(), 0xFFC953BE.toInt())

关键是0xFFC953BE的数值为4291384254,因此应将其存储在Long变量中。但这里的高位是一个符号位,表示一个负数:-3583042,可以存储在Int中。

这就是两种语言之间的区别。在 Kotlin 中,您应该添加-符号来表示否定Int,这在 Java 中是不正确的:

// Kotlin
print(-0x80000000)             // >>> -2147483648 (fits into Int)
print(0x80000000)              // >>>  2147483648 (does NOT fit into Int)

// Java
System.out.print(-0x80000000); // >>> -2147483648 (fits into Integer)
System.out.print(0x80000000);  // >>> -2147483648 (fits into Integer)