2013-07-25 76 views
0

我试图将RGBA值(4值拆分)转换为十六进制值。所以,现在从RGBA到十六进制

int red = Integer.parseInt(colors[0]); 
int green = Integer.parseInt(colors[1]); 
int blue = Integer.parseInt(colors[2]); 
float alpha = Float.parseFloat(colors[3]); 

,我想这些颜色,以十六进制,所以我可以用这个方法来创建一个颜色:

目前,我有这个代码new ColorDrawable(0xFF99CC00)

任何提示?

+0

您是否想通过将'RGBA'转换为'HEX'或任何其他目的在android中设置'setBackgroundColor'?告诉你的实际需要。 – Bishan

+0

我试图将RGBA值(4值拆分)转换为HEX值。 – Reinherd

+0

为什么?设置'setBackgroundColor'或其他用途? – Bishan

回答

1

发现了这件事:

ActionBar bar = this.getActionBar(); 
String hex = String.format("#%02x%02x%02x%02x", alpha,red, green, blue); 
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(hex))); 
2
public int toHex(Color color) { 
    String alpha = pad(Integer.toHexString(color.getAlpha())); 
    String red = pad(Integer.toHexString(color.getRed())); 
    String green = pad(Integer.toHexString(color.getGreen())); 
    String blue = pad(Integer.toHexString(color.getBlue())); 
    String hex = "0x" + alpha + red + green + blue; 
    return Integer.parseInt(hex, 16); 
} 

private static final String pad(String s) { 
    return (s.length() == 1) ? "0" + s : s; 
} 

使用

int color = toHex(new Color(1f, 1f, 1f, 1f)); 

,或者您可以使用

Color.argb(a_int, r_int, g_int, b_int); 
//(Multiply int value by 255.0f)