如何在Android中将颜色整数转换为十六进制字符串?

时间:2011-06-30 19:12:24

标签: java android string colors hex

我有一个从android.graphics.Color

生成的整数

整数的值为-16776961

如何将此值转换为格式为#RRGGBB

的十六进制字符串

简单地说:我想从-16776961输出#0000FF

注意:我不希望输出包含alpha,我也尝试this example但没有成功

11 个答案:

答案 0 :(得分:420)

掩码确保你只获得RRGGBB,%06X为你提供零填充十六进制(总是6个字符长):

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

答案 1 :(得分:50)

答案 2 :(得分:20)

我相信我已找到答案,此代码将整数转换为十六进制字符串并删除alpha。

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

注意只有在您确定删除alpha不会影响任何内容时才使用此代码。

答案 3 :(得分:9)

这就是我做的事情

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

谢谢你们,你们回答了这件事

答案 4 :(得分:4)

使用此方法 Integer.toHexString ,使用Color.parseColor时,某些颜色可能会出现Unknown颜色异常。

使用此方法 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());
    }

答案 5 :(得分:3)

ARGB颜色与十六进制字符串的整数值:

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

十六进制字符串到ARGB颜色的整数值:

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

答案 6 :(得分:1)

如果使用Integer.toHexString,则在转换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

答案 7 :(得分:1)

您可以将其用于没有alpha的颜色:

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

或带有alpha的

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

答案 8 :(得分:0)

String int2string = Integer.toHexString(INTEGERColor); //to ARGB
String HtmlColor = "#"+ int2string.substring(int2string.length() - 6, int2string.length()); // a stupid way to append your color

答案 9 :(得分:0)

在 Koltin 中使用这种方式

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

答案 10 :(得分:0)

如果我们在颜色中使用 alpha,则使用八个“F”而不是六个。

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