2011-03-12 129 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());