【问题标题】:Create color int from alpha, red, green, blue in Android在 Android 中从 alpha、red、green、blue 创建颜色 int
【发布时间】:2019-06-06 10:25:59
【问题描述】:

如何从 alpha、red、green、blue 值(全部从 0 到 255)创建颜色 int?

我需要这个颜色 int 来设置视图的背景颜色。

我试过这个:

    protected int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
        String[] comps = colorString.split("\\+");
        int myColor = 0;
        if(comps.length == 3){
            int a = 255;
            int r = Integer.parseInt(comps[0]);
            int g = Integer.parseInt(comps[1]);
            int b = Integer.parseInt(comps[2]);
            myColor = Color.argb(a, r, g, b);
        } else if (comps.length == 4){
            int a = Integer.parseInt(comps[3]);
            int r = Integer.parseInt(comps[0]);
            int g = Integer.parseInt(comps[1]);
            int b = Integer.parseInt(comps[2]);
            myColor = Color.argb(a, r, g, b);
        }
        return myColor;
    }

但是,当我使用结果设置视图背景颜色时,两个示例 colorString 都是相同的红色???

非常感谢。

【问题讨论】:

  • 你的预期输出是什么,你得到了什么?
  • 你检查过你的索引值吗?您可以反转索引值以获得正确的颜色值

标签: android colors argb


【解决方案1】:

你的代码很完美,没有问题。

有一件事是,如果您将这个“214+13+22+85”值作为整数发送,那么您将获得作为这些值之和的结果值。所以你可能在那里做错了。

import android.graphics.Color;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            textView = findViewById(R.id.txt);
            textView.setTextColor(colorStringToColor("214+13+22+235")); 
        }

        public int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
            String[] comps = colorString.split("\\+");
            int myColor = 0;
            if(comps.length == 3){
                int a = 255;
                int r = Integer.parseInt(comps[0]);
                int g = Integer.parseInt(comps[1]);
                int b = Integer.parseInt(comps[2]);
                myColor = Color.argb(a, r, g, b);
            } else if (comps.length == 4){
                int a = Integer.parseInt(comps[3]);
                int r = Integer.parseInt(comps[0]);
                int g = Integer.parseInt(comps[1]);
                int b = Integer.parseInt(comps[2]);
                myColor = Color.argb(a, r, g, b);
            }
            return myColor;
        }

我在我的项目中实现了你的代码,我检查得很好。我在那里看到的!它正在工作。

【讨论】:

    【解决方案2】:

    您可以使用Color 类中的argb(int red, int green, int blue) 方法来转换它,如下所示:

    int convertedColor= Color.argb(red, green, blue);
    

    并将其设置到您的视图中,如下所示:

    yourView.setBackgroundColor(convertedColor);
    

    【讨论】:

    • 你检查过你进入数组的值是什么吗?我认为拆分存在一些问题。
    • 检查过,没问题
    • 我已经尝试了您的示例,它运行良好。它使用“214+13+22+255”字符串设置红色,而“214+13+22+85”设置其他颜色,如白橙色。
    【解决方案3】:
     Color opaqueRed = Color.valueOf(0xffff0000); // from a color int
     Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f);
    

    【讨论】:

      【解决方案4】:
      int nonTransparentRed = Color.argb(255, 255, 0, 0);
      

      【讨论】:

      • 这是创建 argb 颜色的正确代码。确保您传入的字符串是正确的。要设置您需要在视图上调用 setBackgroundColor 的背景颜色。你能具体说明什么不起作用吗?视图上的颜色转换或颜色设置?
      • 你能确认拆分后你得到了你期望的值吗?
      • 分割得到正确的值。颜色转换不起作用。使用 Color.RED 或 Color.BLUE 可以正常工作。
      • 那么在设置颜色后询问您的视图是否没有被破坏/重绘是有意义的。另外,您确定您的观点得到了正确的参考吗?它设置了错误的颜色还是完全设置了颜色?
      【解决方案5】:

      从您的问题来看,您似乎没有用@ColorInt 注释函数的返回值。

      把你的函数改成

      @ColorInt
      private int colorStringToColor(){...};
      

      另外,在你的函数中,注释

      @ColorInt int myColor = 0;
      

      Android 有android.graphics.Color 类,它提供了对颜色进行操作的方法。

      要从 ARGB 值中获取 ColorInt,您可以使用 Color.argb(int alpha, int red, int green, int blue); 方法,它将返回您对应的ColorInt 值。

      您可以从这里找到有关 Color 类的更多信息

      android.graphics.Color

      【讨论】:

      • 检查我更新的答案,你一定忘了注释你的函数@mrd
      • 注释没有帮助
      【解决方案6】:

      我将继续在这里与 Prince 达成一致,您的代码很好。但是,也提供一个理论。您正在设置视图颜色并且它正在剥离透明度。检查您的号码是否不同?他们应该是。当您将它们输入 setBackgroundColor 时,它们会做同样的事情。如果我没记错的话,它实际上并没有设置 alpha。设置颜色后,尝试剥离 alpha 并在 view.setAlpha(color>>24) 中使用。这在功能上有什么不同吗?

      Prince 将其输入 setTextColor 而不是 setBackgroundColor。

      view.setBackgroundColor(color);
      view.setAlpha(color>>24);
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2015-02-23
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        相关资源
        最近更新 更多