Android:点击生成随机颜色?
我有一个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