生成随机颜色Java [重复]

Posted

技术标签:

【中文标题】生成随机颜色Java [重复]【英文标题】:Generate a Random Color Java [duplicate] 【发布时间】:2013-12-31 21:23:13 【问题描述】:

我正在尝试通过使用随机数生成器为 R、G 和 B 值随机生成数字,并使用这些值生成颜色来创建随机颜色。以下代码在我的onCreate() 方法中:

Random rand = new Random();
    // Java 'Color' class takes 3 floats, from 0 to 1.
    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);

eclipse 怎么会告诉我“构造函数Color(float, float, float) 未定义”?这不应该正常工作吗?

【问题讨论】:

你正确导入了吗? 确保声明“java.awt.Color”:docs.oracle.com/javase/7/docs/api/java/awt/Color.html。另一方面,“android.graphics.Color”没有带有float rgb的构造函数:developer.android.com/reference/android/graphics/… 似乎是 eclipse 自动导入的“android.graphics.Color”。我让它导入“java.awt.Color”,它现在正在工作。谢谢 @ThatGuyThere 编码为 android 对吗? 【参考方案1】:

您应该使用nextInt(int n):int 生成一个介于 0 和 255 之间的随机整数。(请注意,根据 API,Color 方法中不会检查范围,因此如果您自己不限制它,您将结束加上无效的颜色值)

// generate the random integers for r, g and b value
Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);

然后使用静态Color.rgb(r,g,b):int 方法获取一个int 颜色值。 android.graphics.Color 存在的唯一构造函数是无参数构造函数。

int randomColor = Color.rgb(r,g,b);

最后,作为示例,使用setBackgroundColor(int c):void 方法为视图设置颜色背景。

View someView.setBackgroundColor(randomColor);

【讨论】:

public static int rgb 返回类型为int 确实修复了这个问题,并添加了一些关于如何使用 int 颜色值设置背景颜色以查看的更多信息。 帖子现在看起来更完整了。 应该是 rand.nextInt(256)。 nextInt "返回一个伪随机均匀分布在半开范围 [0, n) 中的 int。" 1行代码:view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));【参考方案2】:
public int randomColor(int alpha) 

    int r = (int) (0xff * Math.random());
    int g = (int) (0xff * Math.random());
    int b = (int) (0xff * Math.random());

    return Color.argb(alpha, r, g, b);

有帮助吗?

【讨论】:

【参考方案3】:

http://developer.android.com/reference/android/graphics/Color.html

Color()

构造函数不带任何参数

使用

public static int rgb (int red, int green, int blue)

从红色、绿色、蓝色分量中返回一个 color-int。 alpha 分量是隐式 255(完全不透明)。这些组件值应该是[0..255],但是没有进行范围检查,所以如果超出范围,返回的颜色是未定义的。

参数 red 颜色的红色分量 [0..255] green 颜色的绿色分量 [0..255] blue 颜色的蓝色分量 [0..255]

使用

Random rand = new Random();
int r = rand.nextInt(255);
...// rest of the code  
int randomcolor = Color.rgb(r,g,b); // takes int as param

【讨论】:

【参考方案4】:

利用Color.rgb()方法

Color.rgb((randval)r,(randval)g,(randval)b);

生成随机颜色。

【讨论】:

谢谢。问题实际上出在我的导入上,但这也是一个很好的方法。【参考方案5】:

如果构造函数 Color(float, float, float) 未定义,则将其转换为 int 之类的。

Random rand = new Random();
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
int Red = Integer.parseInt(String.valueOf(r));
int Green= Integer.parseInt(String.valueOf(g));
int Blue= Integer.parseInt(String.valueOf(b));
Color randomColor = new Color(Red , Green, Blue);

但不知道它是否有效,如果不起作用,那么试试这个:

Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
Color randomColor = new Color(r, g, b);

它应该有效,但如果它不起作用,请评论。

【讨论】:

以上是关于生成随机颜色Java [重复]的主要内容,如果未能解决你的问题,请参考以下文章

获取随机颜色 [重复]

每次单击按钮时随机颜色[重复]

如何使用 JavaScript 随机生成 HTML 十六进制颜色代码? [复制]

如何获得随机光色[重复]

我将如何阻止随机颜色随机选择两次相同的颜色[重复]

如何生成特定颜色的随机阴影列表? (例如橙色的随机阴影)