【问题标题】:Cannot use argb color int value in Kotlin?不能在 Kotlin 中使用 argb color int 值?
【发布时间】:2017-06-07 07:49:56
【问题描述】:

当我想在 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);

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

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    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)
    

    【讨论】:

    • 您能否添加一个参考,说明为什么会这样?
    • 是的,这很好奇。
    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2016-11-06
    • 1970-01-01
    相关资源
    最近更新 更多