安卓。点击时生成随机颜色?

115 人关注

我有一个 ImageView ,我在其中以程序化的方式创建可画的东西,并将它们呈现给用户。我的目标是点击上述 ImageView 并改变可画图的颜色。

我怎样才能做到随机变色这一点呢?我目前正在修补 Random() Color.argb() 和其他一些东西,但我似乎无法让它发挥作用

android
colors
imageview
Jared
Jared
发布于 2011-03-12
17 个回答
Lumis
Lumis
发布于 2021-08-26
已采纳
0 人赞同
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);

尽管在你的案例中,你似乎想创建一个新的可画性并将其分配给你的视图。在你的案例中,实际上可画的东西是什么?它是一个图像、形状、填充物...

不是应该到处都是256而不是255吗?nextInt()的API说 "返回一个半开范围[0, n]内的伪随机均匀分布的int"
Kaciula,你是对的,n被排除在外。 docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html
int color = 0xFF000000 | rnd.nextInt(0xFFFFFF); " 只使用1个随机数,而不是3个"
Color.argb函数需要API至少26才能工作。
@That'sEnam 不,有两个Color.argb函数,一个接受int参数,从API第一级开始就存在,你说的那个接受float参数,是的,从API 26开始才有。
Jorgesys
Jorgesys
发布于 2021-08-26
0 人赞同

来获得随机的颜色值,你可以使用这个方法。

public int getRandomColor(){
   Random rnd = new Random();
   return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

然后适用于你的观点。

myView.setBackgroundColor(getRandomColor());
    
ucMedia
ucMedia
发布于 2021-08-26
0 人赞同

因此,如果你正在寻找一个漂亮的调色板,也许使用完全随机的数值并不是一个好主意。 这种方法可能不会产生最好的结果,它总是以选择太深或太亮的类似颜色而告终。

Semi-random approach (Java):

如果你需要一些新鲜亮丽的颜色,那么请使用下面这个简单的类,这是我以前遇到同样问题时写的。它是 semi-random 并使用一个预定义的调色板。

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;

Random approach (Java):

But if you're still considering use random approach你可能想用这一行来代替多行的代码。

int color= ((int)(Math.random()*16777215)) | (0xFF << 24);

使用这个(0xFF << 24)的目的是将阿尔法值设置为最大,即透明度为零。

semi-random (Kotlin)

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

Random approach (Kotlin):

val color = (Math.random() * 16777215).toInt() or (0xFF shl 24)
    
JWL
你的是最好的控制设计案例;-)
paul_f
paul_f
发布于 2021-08-26
0 人赞同

In Kotlin:

val rnd = Random()
val color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
myView.setBackgroundColor(color)
    
确保从 java.util 导入Random
Kevin
Kevin
发布于 2021-08-26
0 人赞同

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

* 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方法在哪里?
@twntee rgb是一个静态方法,见。[ developer.android.com/reference/android/graphics/... , int, int)]
是的,我知道了!实际上,在我的文件中,有多个导入的保存名称?
Gary Davies
Gary Davies
发布于 2021-08-26
0 人赞同
thing.setBackgroundColor(new Random().nextInt());
    
King of Masses
King of Masses
发布于 2021-08-26
0 人赞同

你可以使用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;
    
Amir J
Amir J
发布于 2021-08-26
0 人赞同

这里有一个扩展,可以做同样的事情。

fun Int.Companion.randomColor(): Int
    return Color.argb(255,
                      Random.nextInt(256),
                      Random.nextInt(256),
                      Random.nextInt(256))

而这就是用法。

myTextView.setBackgroundColor(Int.randomColor());
    
abhi
abhi
发布于 2021-08-26
0 人赞同

最简单的解决方案...改变范围以避免深色或浅色......如30至200,以避免黑与白的家庭......

val randomColor: Int
    get() {
        return Color.rgb((30..200).random(),(30..200).random(),(30..200).random())
    
Sumit
Sumit
发布于 2021-08-26
0 人赞同

这是我在一个应用程序中使用的代码,它可能对你有帮助。

它在触摸时产生一个随机的颜色

 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;
    
这到底是做什么的?它看起来是要考虑到触摸的位置。
它将在触摸时改变视图的背景,当你触摸和移动时,它将根据x y位置生成随机颜色并应用于视图。
Raghav Thakkar
Raghav Thakkar
发布于 2021-08-26
0 人赞同
 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);
    
这个代码多次生成蓝色,不是随机的。
Sagar
Sagar
发布于 2021-08-26
0 人赞同

我希望以下两个解决方案可以帮助你。

有两种方法可以通过编程获得随机颜色,以设置为 view

1.First solution

public int randomColor()
         Random random= new Random();
         return Color.argb(255, random.nextInt(256), random.nextInt(256), 
         random.nextInt(256));
  

如果你在滚动中使用adapter,你可能会在同一个view上得到随机的颜色,这可能看起来不好,为了避免这种情况,你可以使用第二个解决方案。

2.Second Solution

你可以根据自己的选择,用ColorGenerator.DEFAULT代替ColorGenerator.MATERIAL。你也可以用任何number来代替position

 ColorGenerator generator = ColorGenerator.MATERIAL; 
    int color = generator.getColor(position);
    holder.mEvent_color_strip.setBackgroundColor(color);
    
那个ColorGenerator是什么?你从哪里得到它?
你从哪里得到ColorGenerator?
Mukesh Patil
Mukesh Patil
发布于 2021-08-26
0 人赞同
bb.setBackgroundColor(Color.rgb(
    getRandomInteger(0,255),
    getRandomInteger(0, 255),
    getRandomInteger(0, 255)
    
Sohaib Aslam
Sohaib Aslam
发布于 2021-08-26
0 人赞同

这个问题的最准确的解决方案。

-首先,在gradle(应用程序)中添加这个。

compile 'com.github.lzyzsd.randomcolor:library:1.0.0'

然后编译并重建该应用程序。

-第二步是通过这种方式使用它。

RandomColor randomColor = new RandomColor();
Button l = findviewbyid(R.id.B1);
l.setBackgroundColor(randomColor.randomColor());

Reference Link:

https://github.com/lzyzsd/AndroidRandomColor

Miramax Mars
Miramax Mars
发布于 2021-08-26
0 人赞同

在你的情况下,你应该像这样做,这对我来说是可行的。

@Override
public void onBindViewHolder(@NonNull WeatherMainAdapter.ViewHolder holder, int position) {
    Random rnd = new Random();
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    holder.date.setText(items.get(position).getDt_txt());
    holder.temp.setText(items.get(position).main.getTemp());
    holder.press.setText(items.get(position).main.getPressure());
    holder.cardView.setBackgroundColor(color);
    
Kamran
Kamran
发布于 2021-08-26
0 人赞同
public static String rndColor()
        Random random = new Random();
        int num = random.nextInt(16777215);
        String hex = "";
        while (num != 0)
            if (num % 16 < 10)
                hex = Integer.toString(num % 16) + hex;
                hex = (char)((num % 16)+55) + hex;
            num = num / 16;
        return "#"+((hex.length()<6)?String.format("%0"+(6-hex.length())+"d", 0):"") + hex;