【问题标题】:Generate a Random Color Java [duplicate]生成随机颜色Java [重复]
【发布时间】: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 android random colors


【解决方案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);
      

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

      【讨论】:

        猜你喜欢
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 2013-02-02
        • 2017-08-09
        • 1970-01-01
        • 2015-11-07
        相关资源
        最近更新 更多