繁体   English   中英

在 Android Studio 中显示 RGB 颜色值

[英]Display RGB color values in Android Studio

我尝试显示图像中的颜色值:颜色:195r 30g 110b

当我每次单击按钮显示随机颜色时,我使用此代码生成 colors 但不显示我想要的

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

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

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

随机#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.

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