반응형
안드로이드에서 색상 정수를 16 진수 문자열로 변환하는 방법은 무엇입니까?
에서 생성 된 정수가 있습니다. android.graphics.Color
정수 값은 -16776961입니다.
이 값을 #RRGGBB 형식의 16 진 문자열로 변환하는 방법
간단히 말하면 : -16776961에서 # 0000FF를 출력하고 싶습니다.
참고 : 출력에 알파가 포함되는 것을 원하지 않으며 성공하지 않고이 예제 를 시도 했습니다.
이 마스크를 사용하면 RRGGBB 만 얻을 수 있으며 % 06X는 0으로 채워진 16 진수 (항상 6 자 길이)를 제공합니다.
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
Integer.toHexString ()을 사용해보십시오
출처 : Java에서 선행 0을 유지하면서 바이트 배열을 16 진수 문자열로 어떻게 변환합니까?
나는 대답을 찾았다 고 생각합니다.이 코드는 정수를 16 진수 문자열로 변환하고 알파를 제거합니다.
Integer intColor = -16895234;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
참고 확실 알파를 제거하면 아무 영향을 미치지 않을 것이라고 경우에만이 코드를 사용합니다.
여기 내가 한 일이 있습니다.
int color=//your color
Integer.toHexString(color).toUpperCase();//upercase with alpha
Integer.toHexString(color).toUpperCase().substring(2);// uppercase without alpha
고마워 당신이 일을 했어
ARGB 색상의 정수 값을 16 진수 문자열로 :
String hex = Integer.toHexString(color); // example for green color FF00FF00
16 진수 문자열을 ARGB 색상의 정수 값으로 :
int color = (Integer.parseInt( hex.substring( 0,2 ), 16) << 24) + Integer.parseInt( hex.substring( 2 ), 16);
이 메소드 Integer.toHexString을 사용하면 Color.parseColor를 사용할 때 일부 색상에 대해 알 수없는 색상 예외가있을 수 있습니다.
그리고이 메서드 String.format ( "# % 06X", (0xFFFFFF & intColor)) 을 사용하면 알파 값이 손실됩니다.
따라서이 방법을 권장합니다.
public static String ColorToHex(int color) {
int alpha = android.graphics.Color.alpha(color);
int blue = android.graphics.Color.blue(color);
int green = android.graphics.Color.green(color);
int red = android.graphics.Color.red(color);
String alphaHex = To00Hex(alpha);
String blueHex = To00Hex(blue);
String greenHex = To00Hex(green);
String redHex = To00Hex(red);
// hexBinary value: aabbggrr
StringBuilder str = new StringBuilder("#");
str.append(alphaHex);
str.append(blueHex);
str.append(greenHex);
str.append(redHex );
return str.toString();
}
private static String To00Hex(int value) {
String hex = "00".concat(Integer.toHexString(value));
return hex.substring(hex.length()-2, hex.length());
}
String int2string = Integer.toHexString(INTEGERColor); //to ARGB
String HtmlColor = "#"+ int2string.substring(int2string.length() - 6, int2string.length()); // a stupid way to append your color
반응형
'Programing' 카테고리의 다른 글
Windows 7 x64의 'adb 기기'를 통해 USB를 통해 Nexus 7을 볼 수 없음 (0) | 2020.05.18 |
---|---|
UITableView를 길게 누름 (0) | 2020.05.18 |
다른 조각으로 데이터를 전송하는 방법? (0) | 2020.05.18 |
쉘에서 변수로 출력을 어떻게 리디렉션합니까? (0) | 2020.05.17 |
프로그래밍 방식으로 스크롤보기를 특정 편집 텍스트로 스크롤하는 방법이 있습니까? (0) | 2020.05.17 |