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)

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...