在Android中通过alpha,红色,绿色,蓝色创建颜色int

时间:2019-06-06 10:25:59

标签: android colors argb

如何从alpha,红色,绿色,蓝色值(都在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都具有相同的红色??

非常感谢。

7 个答案:

答案 0 :(得分:3)

您的代码是完美的,没有问题。

有一件事是,如果将“ 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;
        }

我在您的项目中实现了您的代码,并检查了一下。我在那看到的!它正在工作。

答案 1 :(得分:2)

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

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

并将其设置为这样的视图:

yourView.setBackgroundColor(convertedColor);

答案 2 :(得分:1)

 Color opaqueRed = Color.valueOf(0xffff0000); // from a color int
 Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f);

答案 3 :(得分:0)

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

将功能更改为

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

此外,在您的函数中,注释

@ColorInt int myColor = 0;

Android具有android.graphics.Color类,该类提供在Colors上进行操作的方法。

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

您可以在此处找到有关Color类的更多信息

android.graphics.Color

答案 4 :(得分:0)

int nonTransparentRed = Color.argb(255, 255, 0, 0);

答案 5 :(得分:0)

我要在这里与Prince达成协议,您的代码很好。但是,也提供了一个理论。您正在设置视图颜色,并且正在剥去透明度。检查您的电话号码是否不同?他们应该是。当您将它们输入到setBackgroundColor中时,它们将执行相同的操作。如果我没记错的话,它实际上并没有设置Alpha。设置颜色后,请尝试剥离alpha并在view.setAlpha(color>>24)中使用。功能上有区别吗?

Prince将其馈入setTextColor而不是setBackgroundColor。

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

答案 6 :(得分:-1)

使用:

import android.graphics.Color;
int colorInt = Color.rgb(red, green, blue);

// Set your view's background color
yourView.setBackgroundColor(colorInt);

参数的顺序为红色,绿色,蓝色。