2013-07-25 78 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)