[英]Convert hex color value ( #ffffff ) to integer value
I am receiving hex color values from a server (in this form, #xxxxxx
, example #000000
for black)我从服务器接收十六进制颜色值(在这种形式中,
#000000
#xxxxxx
,例如#000000
表示黑色)
How do I convert this to an integer value?如何将其转换为整数值?
I tried doing Integer.valueOf("0x" + passedColor.substring(1, passedColor.length()))
to get an even more hextastic 0x000000
result, but this isn't intepreted as an int
here, any other suggestions?我试着做
Integer.valueOf("0x" + passedColor.substring(1, passedColor.length()))
来得到一个更十六进制的0x000000
结果,但这在这里没有被解释为int
,还有其他建议吗?
I receive an error: 08-03 21:06:24.673: ERROR/AndroidRuntime(20231): java.lang.NumberFormatException: unable to parse '0x00C8FBFE' as integer
我收到一个错误:
08-03 21:06:24.673: ERROR/AndroidRuntime(20231): java.lang.NumberFormatException: unable to parse '0x00C8FBFE' as integer
I am using the Android SDK for their setBackgroundColor(int color)
function, which takes - as you might have guessed - an integer color value.我将 Android SDK 用于他们的
setBackgroundColor(int color)
函数,它采用 - 正如您可能已经猜到的 - 一个整数颜色值。
This is the OPPOSITE of this question: How to convert a color integer to a hex String in Android?这是这个问题的对立面: How to convert a color integer to a hex String in Android?
The real answer is to use:真正的答案是使用:
Color.parseColor(myPassedColor)
in Android, myPassedColor
being the hex
value like #000
or #000000
or #00000000
. Android 中的
Color.parseColor(myPassedColor)
, myPassedColor
是hex
值,如#000
或#000000
或#00000000
。
However, this function does not support shorthand hex values such as #000
.但是,此函数不支持速记十六进制值,例如
#000
。
Answer is really simple guys, in android if you want to convert hex color in to int, just use android Color class, example shown as below答案非常简单,在android中如果要将十六进制颜色转换为int,只需使用android Color类,示例如下所示
this is for light gray color这是浅灰色
Color.parseColor("#a8a8a8");
Thats it and you will get your result.就是这样,你会得到你的结果。
Integer.parseInt(myString.replaceFirst("#", ""), 16)
I have the same problem that I found some color in form of #AAAAAA
and I want to conver that into a form that android could make use of.我
#AAAAAA
了同样的问题,我发现了一些#AAAAAA
形式的#AAAAAA
,我想将其转换为 android 可以使用的形式。 I found that you can just use 0xFFAAAAAA
so that android could automatically tell the color.我发现您可以只使用
0xFFAAAAAA
以便 android 可以自动分辨颜色。 Notice the first FF
is telling alpha
value.注意第一个
FF
是告诉alpha
值。 Hope it helps希望能帮助到你
The real answer is this simplest and easiest ....真正的答案是这个最简单和最简单的......
String white = "#ffffff";
int whiteInt = Color.parseColor(white);
I was facing the same problem.我面临同样的问题。 This way I was able to solved it.
这样我就能够解决它。 As CQM said, using Color.parseColor() is a good solution to this issue.
正如 CQM 所说,使用 Color.parseColor() 是这个问题的一个很好的解决方案。
Here is the code I used:这是我使用的代码:
this.Button_C.setTextColor(Color.parseColor(prefs.getString("color_prefs", String.valueOf(R.color.green))));
In this case my target was to change the Button's text color (Button_C) when I change the color selection from my Preferences (color_prefs).在这种情况下,我的目标是在更改首选项 (color_prefs) 中的颜色选择时更改按钮的文本颜色 (Button_C)。
Based on CQM 's answer and on ovokerie-ogbeta 's answer to another question I've come up with this solution:基于CQM的回答和ovokerie-ogbeta 对另一个问题的回答,我想出了这个解决方案:
if (colorAsString.length() == 4) { // #XXX
colorAsString = colorAsString.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
}
int color = Color.parseColor(colorAsString);
Get Shared Preferences Color Code in String then Convert to integer and add layout-background color:在字符串中获取共享首选项颜色代码,然后转换为整数并添加布局背景颜色:
sharedPreferences = getSharedPreferences(mypref, Context.MODE_PRIVATE);
String sw=sharedPreferences.getString(name, "");
relativeLayout.setBackgroundColor(Color.parseColor(sw));
Try this, create drawable in your resource...试试这个,在你的资源中创建 drawable ......
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/white"/>
<size android:height="20dp"
android:width="20dp"/>
</shape>
then use...然后用...
Drawable mDrawable = getActivity().getResources().getDrawable(R.drawable.bg_rectangle_multicolor);
mDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
mView1.setBackground(mDrawable);
with color... "#FFFFFF"带有颜色...“#FFFFFF”
if the color is transparent use... setAlpha如果颜色是透明的使用... setAlpha
mView1.setAlpha(x);
with x float 0-1 Ej (0.9f) x 浮动 0-1 Ej (0.9f)
Good Luck祝你好运
if you can pass the values as static const, you can convert the hex value to an Android (android.graphics.Color) using this online converter and put the color reference in the const, ie: color converter give me this value for this color #EE5670 = 0xFFEE5670.如果您可以将值作为静态常量传递,则可以使用此在线转换器将十六进制值转换为 Android (android.graphics.Color) 并将颜色参考放入常量,即:颜色转换器为我提供此颜色的此值#EE5670 = 0xFFEE5670。
static const Color redColor = const Color(0xFFEE5670);
https://convertingcolors.com/hex-color-EE5670.html?search=#EE5670 https://convertingcolors.com/hex-color-EE5670.html?search=#EE5670
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.