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

前端之家收集整理的这篇文章主要介绍了android – 在Kotlin中不能使用argb color int值吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我想在Kotlin中为TextView的textColor设置动画时: @H_403_2@val animator = ObjectAnimator.ofInt(myTextView,"textColor",0xFF8363FF,0xFFC953BE)

发生此错误

@H_403_2@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中是正常的:

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

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

解决方法

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

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

@H_403_2@val animator = ObjectAnimator.ofInt(myTextView,0xFF8363FF.toInt(),0xFFC953BE.toInt())

关键是0xFFC953BE的数值是4291384254,因此它应该存储在Long变量中.但这里的高位是符号位,表示负数:-3583042,可以存储在Int中.

这就是两种语言之间的区别.在Kotlin中你应该添加 – 符号来表示负的Int,这在Java中是不正确的:

@H_403_2@// 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相关文章