微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

当我想在Kotlin中为TextView的textColor设置动画时:
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!,path: Path!): ObjectAnimator! defined in android.animation.ObjectAnimator
public open fun <T : Any!> ofInt(target: TextView!,property: Property<TextView!,vararg values: Int): ObjectAnimator! defined in android.animation.ObjectAnimator
public open fun ofInt(target: Any!,propertyName: String!,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

似乎在Kotlin中不能将值0xFF8363FF和0xFFC953BE强制转换为Int,但是,它在Java中是正常的:

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

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

解决方法

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

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

val animator = ObjectAnimator.ofInt(myTextView,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)

原文地址:https://www.jb51.cc/android/316980.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐