[英]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.