1.原理
堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設服務10余年為成都宣傳片制作小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站定制營銷網(wǎng)站建設商城網(wǎng)站建設手機網(wǎng)站建設小程序網(wǎng)站建設網(wǎng)站改版,從內(nèi)容策劃、視覺設計、底層架構、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設服務。主要是調(diào)用了toHexString(將int類型轉(zhuǎn)為16進制字符串)、parseInt(將字符串解析為int)這兩個方法。
2.代碼
public static void main(String[] args) { String hexString = colorToHexValue(Color.RED); System.out.println("16進制字符串:" + hexString); Color color = fromStrToARGB(hexString); System.out.println("16進制字符串轉(zhuǎn)為顏色的ARGB值:("+String.valueOf(color.getAlpha())+","+String.valueOf(color.getRed())+"," +String.valueOf(color.getGreen())+","+String.valueOf(color.getBlue())+")"); } private static String colorToHexValue(Color color) { return intToHexValue(color.getAlpha()) + intToHexValue(color.getRed()) + intToHexValue(color.getGreen()) + intToHexValue(color.getBlue()); } private static String intToHexValue(int number) { String result = Integer.toHexString(number & 0xff); while (result.length() < 2) { result = "0" + result; } return result.toUpperCase(); } private static Color fromStrToARGB(String str) { String str1 = str.substring(0, 2); String str2 = str.substring(2, 4); String str3 = str.substring(4, 6); String 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); Color color = new Color(red, green, blue, alpha); return color; }