Java 创建一个新的可绘制颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19407672/
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
Create a new color drawable
提问by stacksonstacks
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
我正在尝试将十六进制值转换为 int,以便我可以创建一个新的可绘制颜色。我不确定这是否可行,但根据文档,应该可以。它明确要求
public ColorDrawable (int color)
Added in API level 1 Creates a new ColorDrawable with the specified color.
ParameterscolorThe color to draw.
公共 ColorDrawable (int color)
在 API 级别 1 中添加 创建具有指定颜色的新 ColorDrawable。
参数color要绘制的颜色。
So, my code isn't working because I'm getting an Invalid int: "FF6666" error. Any ideas?
所以,我的代码不起作用,因为我收到了 Invalid int: "FF6666" 错误。有任何想法吗?
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
采纳答案by Enrichman
Since you're talking about hex you have to start with 0x
and don't forget the opacity.
既然你在谈论十六进制,你必须从头开始,0x
不要忘记不透明度。
So basically: 0xFFFF6666
所以基本上:0xFFFF6666
ColorDrawable cd = new ColorDrawable(0xFFFF6666);
You can also create a new colors.xml file into /res and define the colors like:
您还可以在 /res 中创建一个新的 colors.xml 文件并定义颜色,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="mycolor">#FF6666</color>
</resources>
and simply get the color defined in R.color.mycolor
并简单地获取在 R.color.mycolor 中定义的颜色
getResources().getColor(R.color.mycolor)
回答by HpTerm
I think you have to use :
我认为你必须使用:
public static int parseColor (String colorString)
Added in API level 1 Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuschia, lime, maroon, navy, olive, purple, silver, teal
public static int parseColor (String colorString)
API level 1新增 解析颜色字符串,返回对应的color-int。如果无法解析字符串,则抛出 IllegalArgumentException 异常。支持的格式有:#RRGGBB #AARRGGBB 红色、蓝色、绿色、黑色、白色、灰色、青色、洋红色、黄色、浅灰色、深灰色、灰色、浅灰色、深灰色、浅绿色、紫红色、石灰、栗色、海军蓝、橄榄色、紫色、银色,青色
回答by CRUSADER
It should be like this...
应该是这样的...
ColorDrawable cd = new ColorDrawable(0xffff6666);
Note I used 8 hex digits, not 6 hex digit . which add to transparency
注意我使用了 8 个十六进制数字,而不是 6 个十六进制数字。这增加了透明度
回答by JpCrow
For using with ContextCompat and rehuse the color you can do something like this:
要与 ContextCompat 一起使用并重新调整颜色,您可以执行以下操作:
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.white));
回答by BertKing
By followingthe above advice,to be a summary of this question:
通过遵循上述建议,对这个问题做一个总结:
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ce9b2c"));`
ColorDrawable colorDrawable = new ColorDrawable(0xFFCE9B2C); Note there is 8 hex digits, not 6 hex digit,which no work. Case all
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(mContext,R.color.default_color));
ColorDrawable colorDrawable = new ColorDrawable( Color.parseColor("#ce9b2c"));`
ColorDrawable colorDrawable = new ColorDrawable( 0xFFCE9B2C); 请注意,有 8 个十六进制数字,而不是 6 个十六进制数字,这不起作用。案例全部
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(mContext,R.color.default_color));
Selecting up to you!
由你选择!
回答by Rohit Singh
This is how I converted a Hex color to int and applied to a Background of a View
这就是我将十六进制颜色转换为 int 并应用于视图背景的方式
Let's say that we have a color #8080000.
假设我们有一个颜色 #8080000。
1) Hex to int conversion
1) 十六进制到整数的转换
int myColor = Color.parseColor("#808000");
2) Set background
2) 设置背景
view.setBackgroundColor(context.getColor(myColor));