我有一个ImageView
,在其中我以编程方式创建可绘制对象并向用户呈现它们。我的目标是点击该ImageView
并更改可绘制对象的颜色。
我要如何实现随机颜色更改?我目前正在尝试使用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);
尽管在你的情况下,似乎你想创建一个新的drawable并将其分配给你的视图。实际上,在你的情况下,这个drawable是什么?它是一张图片、形状、填充......
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;
}
}
但是如果你仍然考虑使用随机方法
,你可能希望使用这一行代码而不是多行代码:
int color= ((int)(Math.random()*16777215)) | (0xFF << 24);
使用(0xFF << 24)
的目的是将alpha值设置为最大值,即完全不透明。
class RandomColors() {
private val recycle:Stack<Int> = Stack()
private val colors:Stack<Int> = Stack()
init {
recycle.addAll(
Arrays.asList(
// ARGB hex to int >> (0xFFEE5670.toInt(),...)
-0xbbcca, -0x16e19d, -0x63d850, -0x98c549,
-0xc0ae4b, -0xde690d, -0xfc560c, -0xff432c,
-0xff6978, -0xb350b0, -0x743cb6, -0x3223c7,
-0x14c5, -0x3ef9, -0x6800, -0xa8de,
-0x86aab8, -0x616162, -0x9f8275, -0xcccccd
)
)
}
fun getColor(): Int {
if (colors.size == 0)
while (!recycle.isEmpty()) colors.push(recycle.pop())
Collections.shuffle(colors)
val c = colors.pop()
recycle.push(c)
return c
}
}
val color = (Math.random() * 16777215).toInt() or (0xFF shl 24)
fun randomColor(alpha:Int=255) = Color(
Random.nextInt(256),
Random.nextInt(256),
Random.nextInt(256),
alpha = alpha)
val randomColor
get() = Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
val rnd = Random()
val color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
myView.setBackgroundColor(color)
java.util
导入 Random。 - YTerlething.setBackgroundColor(new Random().nextInt());
/**
* 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;
}
这里有一个可以做同样事情的扩展程序:
fun Int.Companion.randomColor(): Int
{
return Color.argb(255,
Random.nextInt(256),
Random.nextInt(256),
Random.nextInt(256))
}
以下是使用方法:
myTextView.setBackgroundColor(Int.randomColor());
你可以使用ColorGenerator来选择随机颜色
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color1 = generator.getRandomColor(); // generate random color
如果您想在重复的同一用户名上使用相同的特定颜色代码,可以像下面这样使用:
public int getColorCode(String userName)
{
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate color based on a key (same key returns the same color), useful for list/grid views
int colorCode = generator.getColor(userName);
return colorCode;
}
最简单的解决方案是改变范围以避免暗色或浅色,例如将范围改为30到200以避免黑色和白色系列。
val randomColor: Int
get() {
return Color.rgb((30..200).random(),(30..200).random(),(30..200).random())
}
val RandomColor
get() = Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))