簡體   English   中英

如何將顏色 integer 轉換為 Android 中的十六進制字符串?

[英]How to convert a color integer to a hex String in Android?

我有一個從 android.graphics.Color 生成的android.graphics.Color

Integer 的值為 -16776961

如何將此值轉換為格式為 #RRGGBB 的十六進制字符串

簡單地說:我想 output #0000FF 從 -16776961

注意:我不希望 output 包含 alpha,我也嘗試過這個示例但沒有成功

掩碼確保您只獲得 RRGGBB,並且 %06X 為您提供零填充十六進制(始終為 6 個字符長):

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

我相信我找到了答案,此代碼將 integer 轉換為十六進制字符串並刪除了 alpha。

Integer intColor = -16895234;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);

請注意,如果您確定刪除 alpha 不會影響任何內容,請僅使用此代碼。

這是我所做的

 int color=//your color
 Integer.toHexString(color).toUpperCase();//upercase with alpha
 Integer.toHexString(color).toUpperCase().substring(2);// uppercase without alpha

謝謝你們的回答做了這件事

Integer ARGB 顏色值轉十六進制字符串:

String hex = Integer.toHexString(color); // example for green color FF00FF00

ARGB 顏色的 integer 值的十六進制字符串:

int color = (Integer.parseInt( hex.substring( 0,2 ), 16) << 24) + Integer.parseInt( hex.substring( 2 ), 16);

使用此方法Integer.toHexString ,在使用 Color.parseColor 時,某些 colors 可能會出現未知顏色異常。

使用這種方法String.format("#%06X", (0xFFFFFF & intColor)) ,您將失去 alpha 值。

所以我推薦這種方法:

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());
    }

您可以將其用於沒有 alpha 的顏色:

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

或者這個與阿爾法:

String hexColor = String.format("#%08X", (0xFFFFFFFF & intColor));

如果您使用Integer.toHexString ,當您轉換 colors (如0xFF000123 )時,您最終會丟失零。 這是我的基於 kotlin 的解決方案,它既不需要 android 特定類也不需要 java。 所以你也可以在多平台項目中使用它:

    fun Int.toRgbString(): String =
        "#${red.toStringComponent()}${green.toStringComponent()}${blue.toStringComponent()}".toUpperCase()

    fun Int.toArgbString(): String =
        "#${alpha.toStringComponent()}${red.toStringComponent()}${green.toStringComponent()}${blue.toStringComponent()}".toUpperCase()

    private fun Int.toStringComponent(): String =
        this.toString(16).let { if (it.length == 1) "0${it}" else it }

    inline val Int.alpha: Int
        get() = (this shr 24) and 0xFF

    inline val Int.red: Int
        get() = (this shr 16) and 0xFF

    inline val Int.green: Int
        get() = (this shr 8) and 0xFF

    inline val Int.blue: Int
        get() = this and 0xFF
String int2string = Integer.toHexString(INTEGERColor); //to ARGB
String HtmlColor = "#"+ int2string.substring(int2string.length() - 6, int2string.length()); // a stupid way to append your color

在 Koltin 中使用這種方式

var hexColor = "#${Integer.toHexString(ContextCompat.getColor(context, R.color.colorTest))}"

修復顏色選擇器庫中的 alpha 問題。

如果您在選擇顏色時獲得 integer 值作為回報,請先將其轉換為十六進制顏色。

    String hexVal = String.format("#%06X", (0xFFFFFFFF & 
    color)).toUpperCase();

    int length=hexVal.length();
    String withoutHash=hexVal.substring(1,length);


    while (withoutHash.length()<=7)
    {

        withoutHash="0"+withoutHash;

    }
    hexVal ="#"+withoutHash;

Simon 的解決方案效果很好,支持 alpha 並且在 R、G、B、A、十六進制中具有前導“零”值的顏色案例不會被忽略。 java 顏色到十六進制字符串轉換的略微修改版本是:

    public static String ColorToHex (Color color) {
    int red = color.getRed();
    int green = color.getGreen();
    int blue = color.getBlue();
    int alpha = color.getAlpha(); 

    String redHex = To00Hex(red);
    String greenHex = To00Hex(green);
    String blueHex = To00Hex(blue);
    String alphaHex = To00Hex(alpha);

    // hexBinary value: RRGGBBAA
    StringBuilder str = new StringBuilder("#");
    str.append(redHex);
    str.append(greenHex);
    str.append(blueHex);
    str.append(alphaHex);

    return str.toString();
}

private static String To00Hex(int value) {
    String hex = "00".concat(Integer.toHexString(value));
    hex=hex.toUpperCase();
    return hex.substring(hex.length()-2, hex.length());
} 

另一種解決方案可能是:

public static String rgbToHex (Color color) {

   String hex = String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
   hex=hex.toUpperCase();
       return hex;
}

您可以輕松使用

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM