簡體   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