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

当我想为Kotlin中TextViewtextColor设置动画时:

 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 

似乎0xFF8363FF0xFFC953BE的值不能转换为Kotlin中的Int ,但是在Java中是正常的:

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

有任何想法吗? 提前致谢。

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)