简体   繁体   English

在 Android Studio 中显示 RGB 颜色值

[英]Display RGB color values in Android Studio

I try to display color values like in the image: COLOR: 195r 30g 110b我尝试显示图像中的颜色值:颜色:195r 30g 110b

when I click on button every time display a random color I use this code for generate colors but doesn't display what I want当我每次单击按钮显示随机颜色时,我使用此代码生成 colors 但不显示我想要的

Random rnd = new Random();
        int color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

        textView.setText("COLOR: " + color);

and this what output displayed这就是 output 显示的在此处输入图像描述

Random#nextInt(int)随机#nextInt(int)

Returns a pseudorandom, uniformly distributed int value between 0 and the specified value.返回介于 0 和指定值之间的伪随机、均匀分布的 int 值。

Color#rgb(int, int, int)颜色#rgb(int,int,int)

Returns a color-int from red, green, blue components.从红色、绿色、蓝色分量返回一个 color-int。 The alpha component is implicity 255 (fully opaque). alpha 分量是隐式 255(完全不透明)。 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.这些组件值应该是 [0..255],但是没有执行范围检查,所以如果它们超出范围,则返回的颜色是未定义的。

Color#rgb(256, 256, 256) - Returns an undefined color because the values are out of range [0..255]. Color#rgb(256, 256, 256) - 返回未定义的颜色,因为值超出范围 [0..255]。

So in your case, you'll need something like this:所以在你的情况下,你需要这样的东西:

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");

Or:或者:

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.

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