1. 원리
주요 메소드는 ToHexString (int 유형을 16 진 스트링으로 변환)과 parseint (문자열을 int로 구문 분석)라고합니다.
2. 코드
public static void main (String [] args) {String hexstring = colortoHexValue (color.red); System.out.println ( "hexstring :" + hexstring); 색상 = FromSttoargb (hexstring); System.out.println ( "hexstring의 argb 값은 색상으로 변환됩니다 :("+string.valueof (color.getalpha ())+","+string.valueof (color.getred ())+","+string.valueof (color.getgreen ())+","+string. valuef ())); } private static string colortoHexValue (color color) {return inttoHexValue (color.getalpha ()) + inttoHexValue (color.getRed ()) + inttoHexValue (color.getGreen ())) + inttoHexValue (color.getBlue ()); } 개인 정적 문자열 inttoHexValue (int number) {문자열 result = integer.toHexString (번호 & 0xff); while (result.length () <2) {result = "0" + result; } return result.toupperCase (); } stroargb (string str) {String str1 = str.substring (0, 2); 문자열 str2 = str.substring (2, 4); 문자열 str3 = str.substring (4, 6); 문자열 str4 = str.substring (6, 8); int alpha = integer.parseint (str1, 16); int red = integer.parseint (str2, 16); int green = integer.parseint (str3, 16); int blue = integer.parseint (str4, 16); 색상 = 새로운 색상 (빨간색, 녹색, 파란색, 알파); 반환 색상; }3. 효과
4. 키 포인트
4.1 ToHexString : 할 말이 없습니다. JDK는 정수를 16 진수로 전환합니다.
4. 따라서 들어오는 데이터가 법적 색상 값임을 확인해야하며 255보다 큰 데이터는 255로 취급되어야합니다. 현재 두 가지 방법이 있습니다. 첫 번째는이 기사의 처리 방법이며, 두 번째는 판단을 추가하는 것입니다. 데이터가 255보다 크면 255와 같으며 적은 경우 처리되지 않습니다. 이에 비해이 백서의 처리 방법은 코드가 가장 적은 처리 방법이며 효율적입니다.
4.3 & : Bitwise "및"Operation. 이진 코드가 모두 1이면 결과는 1이고 그렇지 않으면 0입니다.
4.4 0xff : 0x는 16 진수 뒤에 뒤 따른다는 것을 의미합니다. F는 15이고 2로 변환하면 1111이고, 2 개의 FS는 1111 1111 (10 진수)이고, 처음 24 비트는 0입니다.이 시점에서 작동을 수행하면 0-255 범위의 int 값 만 유효한 색상 값임을 보장합니다.
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.