2011-03-12 132 views
56

我有一个ImageView,其中我正在编程创建drawable并将它们呈现给用户。我的目标是点击说ImageView并更改drawable的颜色。Android:点击生成随机颜色?

我该如何去随机变色位?我目前正在修补Random(),Color.argb()和其他一些事情,但我似乎无法完成它的工作!

+0

请分享您使用的代码。 – Cristian 2011-03-12 03:44:06

回答

235
Random rnd = new Random(); 
paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
view.setBackgroundColor(color); 

虽然你的情况看来,要创建一个新的绘制,并将其分配给您的视图。你的情况实际上是可绘制的?它是一个图像,形状,填充...

+0

这是一个形状。我会在这里尝试一下你的代码。谢谢! – Jared 2011-03-12 15:27:53

+15

难道不是256而不是255吗? nextInt()的API表示“在半开范围[0,n)返回一个伪随机均匀分布int” – 2011-10-28 13:09:32

+0

Kaciula,你是对的,n被排除:http://docs.oracle.com/javase /1.4.2/docs/api/java/util/Random.html – Lumis 2011-12-31 10:20:12

1

我遇到了这一点,这是我的代码,可能会对一些帮助

/** 
* view-source:http://www.kareno.org/js/colors/ 参考 
*Get Random background color and the text color for the background 
* @return 0--》background 
*   1--》text color 
*/ 
public static int[] getRandomColor() { 
    Random random = new Random(); 
    int RGB = 0xff + 1; 
    int[] colors = new int[2]; 
    int a = 256; 
    int r1 = (int) Math.floor(Math.random() * RGB); 
    int r2 = (int) Math.floor(Math.random() * RGB); 
    int r3 = (int) Math.floor(Math.random() * RGB); 
    colors[0] = Color.rgb(r1, r2, r3); 
    if((r1 + r2 + r3) > 450) { 
     colors[1] = Color.parseColor("#222222"); 
    }else{ 
     colors[1] = Color.parseColor("#ffffff"); 
    } 
    return colors; 
} 
+0

和哪里是rgb方法? – 2014-10-06 18:51:27

+0

@twntee rgb是一个静态方法见:[http://developer.android.com/reference/android/graphics/Color.html#rgb(int,int,int)] – acntwww 2014-10-08 04:38:36

+0

是的!实际上在我的文件中有多个导入保存名称? – 2014-10-09 20:02:42

1

这是我的代码应用程序中使用,它可以帮助你。

它产生于触摸随机颜色

public boolean onTouch(View v, MotionEvent event) { 
      int x = (int)event.getX(); 
      int y = (int) event.getY(); 
      float w = v.getWidth(); 

      if(x < (w * (1.0/3))){ 
       layout.setBackgroundColor(Color.rgb(255,x,y)); 
      }else if(x < (w * (2.0/3))){ 
       layout.setBackgroundColor(Color.rgb(x,255,y)); 
      }else{ 
       layout.setBackgroundColor(Color.rgb(x,y,255)); 
      } 
      return true; 
    } 
+0

这到底是什么?它看起来像是要考虑触摸位置 – 2017-12-07 07:54:19

+0

它会改变触摸视图的背景,当您触摸并移动时,它会根据x y位置生成随机颜色并应用于视图 – Sumit 2017-12-07 16:04:39

0
public static int randomColor(){ 
    float[] TEMP_HSL = new float[]{0, 0, 0}; 
    float[] hsl = TEMP_HSL; 
    hsl[0] = (float) (Math.random() * 360); 
    hsl[1] = (float) (40 + (Math.random() * 60)); 
    hsl[2] = (float) (40 + (Math.random() * 60)); 
    return ColorUtils.HSLToColor(hsl); 
} 
-1
bb.setBackgroundColor(Color.rgb(
    getRandomInteger(0,255), 
    getRandomInteger(0, 255), 
    getRandomInteger(0, 255) 
)); 
8

获得随机颜色值,你可以用这个方法:

public int getRandomColor(){ 
    Random rnd = new Random(); 
    return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
} 

然后应用到你的观点:

myView.setBackgroundColor(getRandomColor()); 

enter image description here

0
thing.setBackgroundColor(new Random().nextInt());