Android:点击时生成随机颜色?

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并将其分配给您的视图.在你的情况下,究竟什么是可绘制的?它是图像,形状,填充......

  • 到处都不应该是256而不是255?nextInt()的API表示"返回半开放范围[0,n]中的伪随机均匀分布的int" (15认同)
  • @That'sEnam 不,有两个 Color.argb 函数,一个采用 int 参数,自 API 级别 1 以来就存在,您所说的那个采用 float 参数,是的,仅自 API 26 以来 (2认同)

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)

在此输入图像描述


pau*_*l_f 8

在科特林中:

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)

  • 确保从“java.util”导入随机 (4认同)

ucM*_*dia 8

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.

Semi-random approach :

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)

Random Color Generator class for android


Random approach :

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)

Random Color Generator android

The purpose of using this (0xFF << 24) is to set the alpha value to the maximum that means zero transparency.


acn*_*www 5

我遇到了这个,这是我的代码,可能会有所帮助

 /**
 * 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)


Gar*_*ies 5

thing.setBackgroundColor(new Random().nextInt());
Run Code Online (Sandbox Code Playgroud)