Android: generować losowy kolor po kliknięciu?

Mam ImageView, w którym programowo tworzę drawable i prezentuję je użytkownikowi. Moim celem jest kliknięcie na said ImageView i zmiana koloru rysunku.

Jak mógłbym przejść do losowej zmiany koloru? Aktualnie majstruję przy Random(), Color.argb() i kilka innych rzeczy, ale nie mogę tego uruchomić!

Author: Konstantin Burov, 2011-03-12

9 answers

Random rnd = new Random();
paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

Lub

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
view.setBackgroundColor(color);

Chociaż w Twoim przypadku wydaje się, że chcesz utworzyć nowy drawable i przypisać go do swojego widoku. Co tak naprawdę można wyciągnąć w Twoim przypadku? Czy to obraz, kształt, wypełnienie...

 261
Author: Lumis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-16 18:33:17

Aby uzyskać losowe wartości kolorów, możesz użyć tej metody:

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

Następnie zastosuj do swoich poglądów:

myView.setBackgroundColor(getRandomColor());

Tutaj wpisz opis obrazka

 11
Author: Jorgesys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-09 18:08:28

Spotkałem się z tym i to jest mój kod, może ktoś pomoże

 /**
 * 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;
}
 2
Author: acntwww,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-05 02:42:08

To jest mój kod, którego użyłem w aplikacji, może Ci pomóc.

Generuje losowy kolor na dotyku

 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;
   }
 2
Author: Sumit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-26 15:44:28
thing.setBackgroundColor(new Random().nextInt());
 1
Author: Gary Davies,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-19 03:25:09
 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);
}
 0
Author: Raghav Thakkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-12 09:58:31

Najdokładniejsze rozwiązanie tego problemu:

- najpierw dodaj to w gradle (aplikacja),

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

Następnie skompilować i odbudować aplikację.

- drugi krok po prostu użyj go w ten sposób,

RandomColor randomColor = new RandomColor();

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

Link Referencyjny:

Https://github.com/lzyzsd/AndroidRandomColor

 0
Author: Sohaib Aslam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-20 19:59:30

Mam nadzieję, że poniższe dwa rozwiązania mogą Ci pomóc.

Istnieją dwa sposoby programowania losowych kolorów, aby ustawić je na view

1.Pierwsze rozwiązanie

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

Jeśli używasz w adapter na przewijaniu możesz uzyskać losowe kolory dla tego samego view może to nie wyglądać dobrze, aby tego uniknąć, możesz użyć drugiego rozwiązania.

2.Drugie Rozwiązanie

Możesz użyć ColorGenerator.DEFAULT zamiast ColorGenerator.MATERIAL zgodnie z Twoim wyborem.Możesz również użyć dowolnego number zamiast position

 ColorGenerator generator = ColorGenerator.MATERIAL; 
    int color = generator.getColor(position);
    holder.mEvent_color_strip.setBackgroundColor(color);
 0
Author: Sagar Hudge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-24 10:39:31
bb.setBackgroundColor(Color.rgb(
    getRandomInteger(0,255),
    getRandomInteger(0, 255),
    getRandomInteger(0, 255)
));
 -1
Author: Mukesh Patil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-23 03:26:30