生成随机颜色 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20560899/
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
Generate a Random Color Java
提问by ThatGuyThere
I am trying to create a random color by randomly generating numbers for R,G, and B values with a random number generator, and using the values to make a color. The following code is in my onCreate()
method:
我试图通过使用随机数生成器随机生成 R、G 和 B 值的数字并使用这些值来创建颜色来创建随机颜色。以下代码在我的onCreate()
方法中:
Random rand = new Random();
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
How come eclipse tells me "The constructor Color(float, float, float)
is undefined"? Shouldn't this work correctly?
eclipse怎么会告诉我“构造函数Color(float, float, float)
未定义”?这不应该正常工作吗?
采纳答案by hcpl
You should use nextInt(int n):intto generate a random integer between 0 and 255. (note that according to API the range is not checked within the Color methods so if you don't limit it yourself you'll end up with invalid color values)
您应该使用nextInt(int n):int生成 0 到 255 之间的随机整数。(请注意,根据 API,在 Color 方法中不会检查范围,因此如果您不自己限制它,您最终会得到无效的颜色值)
// generate the random integers for r, g and b value
Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
Then get an int color value with the static Color.rgb(r,g,b):intmethod. The only constructor that exists for android.graphics.Color
is a non argument constructor.
然后使用静态Color.rgb(r,g,b):int方法获取 int 颜色值。唯一存在的android.graphics.Color
构造函数是非参数构造函数。
int randomColor = Color.rgb(r,g,b);
Finally, as an example, use the setBackgroundColor(int c):voidmethod to set a color background to a view.
最后,作为示例,使用setBackgroundColor(int c):void方法为视图设置颜色背景。
View someView.setBackgroundColor(randomColor);
回答by Nambi
Makeuse of Color.rgb()method
利用Color.rgb()方法
Color.rgb((randval)r,(randval)g,(randval)b);
to generate random color.
生成随机颜色。
回答by venciallee
public int randomColor(int alpha) {
int r = (int) (0xff * Math.random());
int g = (int) (0xff * Math.random());
int b = (int) (0xff * Math.random());
return Color.argb(alpha, r, g, b);
}
can it help?
它有帮助吗?
回答by Raghunandan
http://developer.android.com/reference/android/graphics/Color.html
http://developer.android.com/reference/android/graphics/Color.html
Color()
Color()
Constructor does not take any params
构造函数不接受任何参数
Use
用
public static int rgb (int red, int green, int blue)
public static int rgb (int red, int green, int blue)
Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.
从红、绿、蓝分量返回一个颜色整数。alpha 分量是隐式 255(完全不透明)。这些组件值应该是 [0..255],但是没有执行范围检查,所以如果它们超出范围,返回的颜色是未定义的。
Parameters red Red component [0..255] of the color green Green component [0..255] of the color blue Blue component [0..255] of the color
参数 red 红色分量 [0..255] 绿色绿色分量 [0..255] 蓝色蓝色分量 [0..255] 颜色
Use
用
Random rand = new Random();
int r = rand.nextInt(255);
...// rest of the code
int randomcolor = Color.rgb(r,g,b); // takes int as param
回答by Cropper
If The constructor Color(float, float, float) is undefined than convert it into int like.
如果构造函数 Color(float, float, float) 未定义,则将其转换为 int 之类的。
Random rand = new Random();
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
int Red = Integer.parseInt(String.valueOf(r));
int Green= Integer.parseInt(String.valueOf(g));
int Blue= Integer.parseInt(String.valueOf(b));
Color randomColor = new Color(Red , Green, Blue);
But Don't know that it works or not if doesn't work then try this:
但是不知道它是否有效,如果不起作用,请尝试以下操作:
Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
Color randomColor = new Color(r, g, b);
It should works but if it doesn't works then comment.
它应该有效,但如果无效,请发表评论。