简体   繁体   English

将argb数组传递给int或long

[英]Passing argb array to int or long

I have a method that gives me an array with the ARGB parts of the color I need to paint into a canvas gradient. 我有一个方法,可以给我一个数组,其中包含我需要绘制到画布渐变中的颜色的ARGB部分。 But as far as I know this gradient only accepts hexadecimal number representing colors. 但据我所知,此渐变仅接受代表颜色的十六进制数。 So I did a function based on the info I found here. 所以我根据在这里找到的信息做了一个功能。

This is the function: 这是功能:

public static long getIntegerHexFromARGB(int a, int r, int g, int b){
    String hex = String.format("#%02x%02x%02x%02x", a, r, g, b);
    return Long.parseLong(hex,16);
}

And this is how I call it: 这就是我所说的:

long rgba_outter_circle = FormulaHelpers.getIntegerHexFromARGB(argbCircleColor[0], argbCircleColor[1], argbCircleColor[2], argbCircleColor[3]);

My problem is that this code is inside a custom view I´m doing for including it inside a layout, and the Android Studio layout editor claims about this: 我的问题是,这段代码位于我正在将其包含在布局中的自定义视图中,而Android Studio布局编辑器对此声明如下:

java.lang.NumberFormatException: For input string: "#64FF0000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:430)
at java.lang.Long.valueOf(Long.java:513)
at math.FormulaHelpers.getIntegerHexFromARGB(FormulaHelpers.java:41)
at framework.joystickController.JoystickButtonView.onMeasure(JoystickButtonView.java:177)
at android.view.View.measure(View.java:17430)
... and much more

The line it claims is FormulaHelpers:41, that is: 它声称的行是FormulaHelpers:41,即:

return Long.parseLong(hex,16);

Can somebody see what I´m doing wrong? 有人可以看到我在做什么吗? I can't find the problem anywhere Sorry for my english 我在任何地方都找不到问题对不起我的英语

Correct hex number in Java starts with '0x', not with '#'. Java中正确的十六进制数字以“ 0x”开头,而不是以“#”开头。 Your number should be "0x64FF0000", and the format should be "0x%02x%02x%02x%02x". 您的电话号码应为“ 0x64FF0000”,格式应为“ 0x%02x%02x%02x%02x”。 You can also create an int using formula: 您还可以使用公式创建一个int:

int color = (a<<24) | (r<<16) | (g<<8) | b

This method should work as well: 此方法也应该工作:

Color.argb(a,r,g,b)

如果希望将其解析为长格式,请从十六进制字符串中删除#:IE

String hex = String.format("%02x%02x%02x%02x", a, r, g, b);

It looks like you want to use this method from Color : 看来您想在Color使用此方法:

public static int parseColor (String colorString)

I think it will do exactly what you want. 我认为它将完全满足您的要求。

It will work on strings with the format you use right now, but also take color names. 它适用于您现在使用的格式的字符串,但也可以使用颜色名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM