반응형
안드로이드 뷰에서 색상을 지정하려면 drawable, resource, rgb 값이어야 합니다.
일단 drawable, resource는 아닙니다. 그럼 int형인 rgb값을 지정해야 합니다.
int형 rgb값은 Color.rgb(int r, int g, int b) 로 얻을 수 있습니다.
r,g,b 값을 따로따로 int형으로 줘야 합니다. 우리가 가지고 있는 16진수 문자열인데....
Integer.parseInt()로 변환도 안되고... 직접 만들어야 겠습니다!
import java.util.Hashtable;
import android.graphics.Color;
public class ColorString {
private Hashtable hexTable = null;
public ColorString(){
hexTable = new Hashtable();
hexTable.put('0', 0);
hexTable.put('1', 1);
hexTable.put('2', 2);
hexTable.put('3', 3);
hexTable.put('4', 4);
hexTable.put('5', 5);
hexTable.put('6', 6);
hexTable.put('7', 7);
hexTable.put('8', 8);
hexTable.put('9', 9);
hexTable.put('A', 10);
hexTable.put('B', 11);
hexTable.put('C', 12);
hexTable.put('D', 13);
hexTable.put('E', 14);
hexTable.put('F', 15);
}
public int toRGB(String color){
String strR = color.substring(1, 3);
String strG = color.substring(3, 5);
String strB = color.substring(5);
return Color.rgb(getHexValue(strR), getHexValue(strG), getHexValue(strB));
}
private int getHexValue(String num){
return hexTable.get(num.charAt(0)) * 15 + hexTable.get(num.charAt(1));
}
}
hashtable을 안 써도 되긴 하지만.... 그리고 static이 더 편할 수도 있지만....
그냥 만들다 보니 이렇게 됐네요 ㅋ
반응형
'Programming > Android' 카테고리의 다른 글
| smoothScrollTo 가 적용이 되지 않을때 (4) | 2011.01.25 |
|---|---|
| Custom ListPreference #3 - onPrepareDialogBuilder (0) | 2011.01.21 |
| 같은 Activity 안에서 상황에 따라 다른 OptionMenu를 보여주고 싶어요! (0) | 2011.01.19 |
| Custom ListPreference #2 - onCreateDialogView (0) | 2011.01.14 |
| Custom ListPreference #1 (0) | 2011.01.14 |