android的颜色值转换问题。

小李葛格 2016-04-19 03:31:16
我们知道Color.parseColor();方法可将#FF0000转换成int类型的颜色

那么请问如何将int类型的颜色转换回这种网页适用的颜色?

比如-16777216转换为#000000

...全文
217 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小李葛格 2016-04-19
  • 打赏
  • 举报
回复
引用 3 楼 u014027694 的回复:
有些地方有些多余,你自己去掉就好了
膜拜大神
节操在此 2016-04-19
  • 打赏
  • 举报
回复
有些地方有些多余,你自己去掉就好了
节操在此 2016-04-19
  • 打赏
  • 举报
回复
int a = Color.alpha(-16777216);
    	int r = Color.red(-16777216);
    	int g = Color.green(-16777216);
    	int b = Color.blue(-16777216);
    	byte[] colorBytes = new byte[4];
    	colorBytes[0] = (byte) (a & 0xff);
    	colorBytes[1] = (byte) (r & 0xff);
    	colorBytes[2] = (byte) (g & 0xff);
    	colorBytes[3] = (byte) (b & 0xff);
    	StringBuilder builder = new StringBuilder();
    	builder.append("#");
    	builder.append(bytesToHexString(colorBytes));
    	String colorString = builder.toString();
节操在此 2016-04-19
  • 打赏
  • 举报
回复

 public static String bytesToHexString(byte[] b) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF).toUpperCase();
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			str.append(hex);
		}
		return str.toString().toUpperCase();
}

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧