Jar*_*red 88 android colors imageview
我有一个ImageView
,我在程序上创建drawables并将它们呈现给用户.我的目标是点击说明ImageView
并改变drawable的颜色.
我怎么去随机变色位?我目前正在修补Random()
,Color.argb()
还有一些其他的事情,但我似乎无法让它工作!
Lum*_*mis 307
Random rnd = new Random();
paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Run Code Online (Sandbox Code Playgroud)
要么
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
view.setBackgroundColor(color);
Run Code Online (Sandbox Code Playgroud)
虽然在您的情况下,您似乎想要创建一个新的drawable并将其分配给您的视图.在你的情况下,究竟什么是可绘制的?它是图像,形状,填充......
Jor*_*sys 16
要获得随机颜色值,您可以使用此方法:
public int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
Run Code Online (Sandbox Code Playgroud)
然后适用于您的观点:
myView.setBackgroundColor(getRandomColor());
Run Code Online (Sandbox Code Playgroud)
在科特林中:
val rnd = Random()
val color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
myView.setBackgroundColor(color)
Run Code Online (Sandbox Code Playgroud)
So if you’re looking for a beautiful color palette, Maybe It's Not Such A Great Idea To use totally random values.
This approach might not yield the best results, It always ends up with a selection of similar colors that way too dark or way too bright.
If you need some fresh and shiny colors then use the following simple class, that I wrote previously when I had the same issues. It's semi-random
and uses a predefined color palette:
class RandomColors {
private Stack<Integer> recycle, colors;
public RandomColors() {
colors = new Stack<>();
recycle =new Stack<>();
recycle.addAll(Arrays.asList(
0xfff44336,0xffe91e63,0xff9c27b0,0xff673ab7,
0xff3f51b5,0xff2196f3,0xff03a9f4,0xff00bcd4,
0xff009688,0xff4caf50,0xff8bc34a,0xffcddc39,
0xffffeb3b,0xffffc107,0xffff9800,0xffff5722,
0xff795548,0xff9e9e9e,0xff607d8b,0xff333333
)
);
}
public int getColor() {
if (colors.size()==0) {
while(!recycle.isEmpty())
colors.push(recycle.pop());
Collections.shuffle(colors);
}
Integer c= colors.pop();
recycle.push(c);
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
But if you're still considering use random approach
you may want use this single line instead of multiple lines of code :
int color= ((int)(Math.random()*16777215)) | (0xFF << 24);
Run Code Online (Sandbox Code Playgroud)
The purpose of using this (0xFF << 24)
is to set the alpha value to the maximum that means zero transparency.
我遇到了这个,这是我的代码,可能会有所帮助
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)