1.原理
主要是調(diào)用了toHexString(將int類型轉(zhuǎn)為16進(jìn)制字符串)、parseInt(將字符串解析為int)這兩個(gè)方法。
2.代碼
public static void main(String[] args) { String hexString = colorToHexValue(Color.RED); System.out.println("16進(jìn)制字符串:" + hexString); Color color = fromStrToARGB(hexString); System.out.println("16進(jìn)制字符串轉(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; }
網(wǎng)站名稱:Java中Color和16進(jìn)制字符串互相轉(zhuǎn)換的方法-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article46/dgjshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、外貿(mào)建站、自適應(yīng)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容