【问题标题】:Negative int color value负 int 颜色值
【发布时间】:2016-12-12 11:57:29
【问题描述】:

我有问题。我有一个自定义对象,其字段如下

private String id;
private int key;
private String name;
private String desc;
private int sortorder;
private int color;

和构造函数一样

 public Label(String id, int key, String name, String desc, int sortorder, int color)

我在创建这样的对象时添加颜色

Color.rgb(dialogColorRed, dialogColorGreen, dialogColorBlue)

其中 dialogColorRed、dialogColorGreen、dialogColorBlue - 随机分配的值从 0 到 255。

然后我在 JSON 中打包我的对象的 ArrayList,对象看起来像这样(注意颜色字段)

"color": -6508994,
    "desc": "",
    "id": "81fed08a336b185e226a93f199f34803",
    "key": 87,
    "name": "w",
    "sortorder": 0

我得到负颜色 int 值,但我需要这样的东西

 "color":"4278255360"

有什么问题?

【问题讨论】:

  • 你怎么需要这样的东西?这如何转化为颜色?
  • Java 没有无符号整数,你的颜色的 alpha 通道会翻转 32 位整数的符号位。
  • @ItamarGreen 这将被其他程序转换。我只需要包装这样的颜色
  • @laalto 你有什么推荐的?
  • 我仍然没有发现问题。当解析回 32 位 int 时,负值将用表示相同颜色的相同位模式表示。

标签: java android colors rgb


【解决方案1】:

您好,很晚了,但是您尝试过吗,私人长密钥;而不是私有 int 密钥; 解释 : 您用来存储颜色代码的键可能比整数可以容纳的大。

整数变量的有效范围是 -2147483648 到 +2147483647。

                       2147483647 MAX

而你想要这个数字:4278255360。当一个整数达到最大值 2147483647 + 1 = -2147483648 时,它的新值现在是最小值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2021-06-09
    • 2014-01-17
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多