[英]how to set the "Color.rgb" in Java to random?
I'm experimenting with Android studio and I wanted to pass a random value to the color.rgb.我正在试验 Android studio,我想将一个随机值传递给 color.rgb。 This is the code that I tried
这是我试过的代码
int x = rand.nextInt(9);
int y = rand.nextInt(9);
int z = rand.nextInt(9);
viewTest.setBackgroundColor(Color.rgb(x,y,z));
edit: the color range is between 0 and 255 so to right code is编辑:颜色范围在 0 到 255 之间,所以右边的代码是
int x = rand.nextInt(255);
int y = rand.nextInt(255);
int z = rand.nextInt(255);
viewTest.setBackgroundColor(Color.rgb(x,y,z));
not the best solution but it works Thanks to jon Skeet for remembering me :)不是最好的解决方案,但它有效感谢jon Skeet记住我:)
You can generate a random color like this:您可以生成这样的随机颜色:
Random rng = new Random();
int red = rng.nextInt(256);
int green = rng.nextInt(256);
int blue = rng.nextInt(256);
viewTest.setBackgroundColor(Color.rgb(red, green, blue));
Using nextInt(266)
means that you will get a random int
from 0 to 255.使用
nextInt(266)
意味着您将获得一个从 0 到 255 的随机int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.