Jak przekonwertować liczbę całkowitą koloru na ciąg szesnastkowy w Androidzie?

Mam liczbę całkowitą, która została wygenerowana z android.graphics.Color

Liczba całkowita ma wartość -16776961

Jak przekonwertować tę wartość na łańcuch szesnastkowy w formacie #RRGGBB

Po prostu: chciałbym wyjść # 0000ff z -16776961

Uwaga: nie chcę, aby wyjście zawierało Alfę i próbowałem również ten przykład bez powodzenia

Author: Community, 2011-06-30

7 answers

Maska upewnia się, że otrzymujesz tylko RRGGBB, a %06X daje zero-padded hex (zawsze 6 znaków):

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
 388
Author: Josh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-30 19:56:32
 45
Author: ming_codes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:26

Wydaje mi się, że znalazłem odpowiedź, ten kod zamienia liczbę całkowitą na ciąg szesnastkowy i usuwa Alfę.

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

Uwaga używaj tego kodu tylko wtedy, gdy jesteś pewien, że usunięcie Alfy nie wpłynie na nic.

 18
Author: Bosah Chude,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-01 06:39:38

Oto co zrobiłem

 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

 7
Author: Diljeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-17 17:53:36

Z tą metodą Integer.toHexString, możesz mieć wyjątek nieznanego koloru dla niektórych kolorów podczas używania koloru.parseColor.

I za pomocą tej metody String.format ("#%06X", (0xFFFFFF & intColor)) , stracisz wartość alfa.

Więc polecam tę metodę:

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());
    }
 2
Author: Simon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-30 00:35:04

Wartość całkowita koloru ARGB do ciągu szesnastkowego:

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

Szesnastkowy łańcuch do liczby całkowitej koloru ARGB:

int color = (Integer.parseInt( hex.substring( 0,2 ), 16) << 24) + Integer.parseInt( hex.substring( 2 ), 16);
 1
Author: Style-7,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 14:21:21
String int2string = Integer.toHexString(INTEGERColor); //to ARGB
String HtmlColor = "#"+ int2string.substring(int2string.length() - 6, int2string.length()); // a stupid way to append your color
 0
Author: chundk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-14 23:33:37