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

Hon*_*uan 9 android kotlin

当我想动画textColorTextView在科特林:

val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF, 0xFFC953BE)
Run Code Online (Sandbox Code Playgroud)

发生此错误:

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
Run Code Online (Sandbox Code Playgroud)

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

ObjectAnimator animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF, 0xFFC953BE);
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*nov 16

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

你必须Int明确地将它们强制转换:

val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF.toInt(), 0xFFC953BE.toInt())
Run Code Online (Sandbox Code Playgroud)

的一点是,的数值0xFFC953BE4291384254,所以应该被存储在一个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)
Run Code Online (Sandbox Code Playgroud)