程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-轉換顏色RGB到HEX

java-轉換顏色RGB到HEX

編輯:編程綜合問答
轉換顏色RGB到HEX

創建了一個方法轉換三原色值到HEX值:

protected String convertRGBToHex(int r, int g, int b) {    
    String rFString, rSString, gFString, gSString, bFString, bSString, result;
    int red, green, blue;
    int rred, rgreen, rblue;
    red = r / 16;
    rred = r % 16;
    if (red == 10) rFString = "A";
    else if (red == 11) rFString = "B";
    else if (red == 12) rFString = "C";
    else if (red == 13) rFString = "D";
    else if (red == 14) rFString = "E";
    else if (red == 15) rFString = "F";
    else rFString = String.valueOf(red);

    if (rred == 10) rSString = "A";
    else if (rred == 11) rSString = "B";
    else if (rred == 12) rSString = "C";
    else if (rred == 13) rSString = "D";
    else if (rred == 14) rSString = "E";
    else if (rred == 15) rSString = "F";
    else rSString = String.valueOf(rred);

    rFString = rFString + rSString;

    green = g / 16;
    rgreen = g % 16;

    if (green == 10) gFString = "A";
    else if (green == 11) gFString = "B";
    else if (green == 12) gFString = "C";
    else if (green == 13) gFString = "D";
    else if (green == 14) gFString = "E";
    else if (green == 15) gFString = "F";
    else gFString = String.valueOf(green);

    if (rgreen == 10) gSString = "A";
    else if (rgreen == 11) gSString = "B";
    else if (rgreen == 12) gSString = "C";
    else if (rgreen == 13) gSString = "D";
    else if (rgreen == 14) gSString = "E";
    else if (rgreen == 15) gSString = "F";
    else gSString = String.valueOf(rgreen);

    gFString = gFString + gSString;

    blue = b / 16;
    rblue = b % 16;

    if (blue == 10) bFString = "A";
    else if (blue == 11) bFString = "B";
    else if (blue == 12) bFString = "C";
    else if (blue == 13) bFString = "D";
    else if (blue == 14) bFString = "E";
    else if (blue == 15) bFString = "F";
    else bFString = String.valueOf(blue);

    if (rblue == 10) bSString = "A";
    else if (rblue == 11) bSString = "B";
    else if (rblue == 12) bSString = "C";
    else if (rblue == 13) bSString = "D";
    else if (rblue == 14) bSString = "E";
    else if (rblue == 15) bSString = "F";
    else bSString = String.valueOf(rblue);
    bFString = bFString + bSString;
    result = "#" + rFString + gFString + bFString;
    return result;

}

我想知道能不能將這個string結果轉換到int值,使用方法:

view.setBackgroundColor(0xF82619);

或者轉換string到int:

view.setBackgroundColor(result);

最佳回答:


view.setBackgroundColor(Color.parseColor("#0xF82619"));
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved