我有一個ImageView
,其中我正在編程創建drawable並將它們呈現給用戶。我的目標是點擊說ImageView
並更改drawable的顏色。Android:點擊生成隨機顏色?
我該如何去隨機變色位?我目前正在修補Random()
,Color.argb()
和其他一些事情,但我似乎無法完成它的工作!
我有一個ImageView
,其中我正在編程創建drawable並將它們呈現給用戶。我的目標是點擊說ImageView
並更改drawable的顏色。Android:點擊生成隨機顏色?
我該如何去隨機變色位?我目前正在修補Random()
,Color.argb()
和其他一些事情,但我似乎無法完成它的工作!
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);
雖然你的情況看來,要創建一個新的繪製,並將其分配給您的視圖。你的情況實際上是可繪製的?它是一個圖像,形狀,填充...
我遇到了這一點,這是我的代碼,可能會對一些幫助
/**
* 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;
}
和哪裏是rgb方法? – 2014-10-06 18:51:27
@twntee rgb是一個靜態方法見:[http://developer.android.com/reference/android/graphics/Color.html#rgb(int,int,int)] – acntwww 2014-10-08 04:38:36
是的!實際上在我的文件中有多個導入保存名稱? – 2014-10-09 20:02:42
這是我的代碼應用程序中使用,它可以幫助你。
它產生於觸摸隨機顏色
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;
}
這到底是什麼?它看起來像是要考慮觸摸位置 – 2017-12-07 07:54:19
它會改變觸摸視圖的背景,當您觸摸並移動時,它會根據x y位置生成隨機顏色並應用於視圖 – Sumit 2017-12-07 16:04:39
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);
}
bb.setBackgroundColor(Color.rgb(
getRandomInteger(0,255),
getRandomInteger(0, 255),
getRandomInteger(0, 255)
));
thing.setBackgroundColor(new Random().nextInt());
請分享您使用的代碼。 – Cristian 2011-03-12 03:44:06