如何在 Android 中将颜色整数转换为十六进制字符串? [英] How to convert a color integer to a hex String in Android?

查看:73
本文介绍了如何在 Android 中将颜色整数转换为十六进制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 android.graphics.Color

整数的值为 -16776961

The Integer has a value of -16776961

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

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

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

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

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

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

推荐答案

掩码确保你只得到 RRGGBB,而 %06X 给你零填充的十六进制(总是 6 个字符长):

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

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

这篇关于如何在 Android 中将颜色整数转换为十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆