【发布时间】: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 都是相同的红色???
非常感谢。
【问题讨论】:
-
你的预期输出是什么,你得到了什么?
-
你检查过你的索引值吗?您可以反转索引值以获得正确的颜色值