创建一个新的可绘制颜色 [英] Create a new color drawable
问题描述
我正在尝试将十六进制值转换为int,以便创建新的可绘制颜色.我不确定是否可行,但是根据文档 , 它应该.显然是要
I am trying to convert a hex value to an int so I can create a new color drawable. I'm not sure if this is possible, but according to the documentation, it should. It plainly asks for
公共ColorDrawable(int颜色)
已添加到API级别1中,并使用指定的颜色创建了一个新的ColorDrawable 颜色.
Added in API level 1 Creates a new ColorDrawable with the specified color.
参数 颜色要绘制的颜色.
因此,我的代码无法正常工作,因为我收到了一个无效的int:"FF6666"错误.有什么想法吗?
So, my code isn't working because I'm getting an Invalid int: "FF6666" error. Any ideas?
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
推荐答案
由于您在谈论十六进制,因此必须从0x
开始,并且不要忘记不透明性.
Since you're talking about hex you have to start with 0x
and don't forget the opacity.
所以基本上: 0xFFFF6666
ColorDrawable cd = new ColorDrawable(0xFFFF6666);
您还可以在/res中创建一个新的colors.xml文件,并定义以下颜色:
You can also create a new colors.xml file into /res and define the colors like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="mycolor">#FF6666</color>
</resources>
,只需获取R.color.mycolor中定义的颜色
and simply get the color defined in R.color.mycolor
getResources().getColor(R.color.mycolor)
这篇关于创建一个新的可绘制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!