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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6539879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:34:53  来源:igfitidea点击:

How to convert a color integer to a hex String in Android?

javaandroidstringcolorshex

提问by Bosah Chude

I have an integer that was generated from an android.graphics.Color

我有一个整数,它是从一个 android.graphics.Color

The Integer has a value of -16776961

整数的值为 -16776961

How do I convert this value into a hex string with the format #RRGGBB

如何将此值转换为格式为 #RRGGBB 的十六进制字符串

Simply put: I would like to output #0000FF from -16776961

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

Note:I do not want the output to contain an alpha and i have also tried this examplewithout any success

注意:我不希望输出包含 alpha 并且我也尝试过这个例子但没有成功

采纳答案by Josh

The mask makes sure you only get RRGGBB, and the %06X gives you zero-padded hex (always 6 chars long):

掩码确保您只获得 RRGGBB,而 %06X 为您提供零填充的十六进制(始终为 6 个字符长):

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

回答by Bosah Chude

I believe i have found the answer, This code converts the integer to a hex string an removes the alpha.

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

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

Noteonly use this code if you are sure that removing the alpha would not affect anything.

请注意,仅当您确定删除 alpha 不会影响任何内容时才使用此代码。

回答by Diljeet

Here is what i did

这是我所做的

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

Thanks guys you answers did the thing

谢谢你们,你们的回答做到了

回答by chundk

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

回答by Simon

With this method Integer.toHexString, you can have an Unknown color exception for some colors when using Color.parseColor.

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

And with this method String.format("#%06X", (0xFFFFFF & intColor)), you'll lose alpha value.

使用此方法String.format("#%06X", (0xFFFFFF & intColor)),您将丢失 alpha 值。

So I recommend this method:

所以我推荐这个方法:

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

回答by Style-7

Integer value of ARGB color to hexadecimal string:

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

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

Hexadecimal string to integer value of ARGB color:

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

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