[英]How do I convert 3 int rgb values into one int value of color in android studio?
[英]Display RGB color values in Android Studio
随机#nextInt(int)
返回介于 0 和指定值之间的伪随机、均匀分布的 int 值。
颜色#rgb(int,int,int)
从红色、绿色、蓝色分量返回一个 color-int。 alpha 分量是隐式 255(完全不透明)。 这些组件值应该是 [0..255],但是没有执行范围检查,所以如果它们超出范围,则返回的颜色是未定义的。
Color#rgb(256, 256, 256) - 返回未定义的颜色,因为值超出范围 [0..255]。
所以在你的情况下,你需要这样的东西:
Random rnd = new Random();
int red = rnd.nextInt(255);
int green = rnd.nextInt(255);
int blue = rnd.nextInt(255);
int color = Color.rgb(red, green, blue);
textView.setText("COLOR: " + red + "r " + green + "g " + blue + "b");
或者:
Random rnd = new Random();
int color = Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
textView.setText("COLOR: " + ((color >> 16) & 0xFF) + "r " + ((color >> 8) & 0xFF) + "g " + (color & 0xFF) + "b");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.