Android:点击时生成随机颜色? [英] Android: Generate random color on click?
问题描述
我有一个 ImageView
,我在其中以编程方式创建可绘制对象并将它们呈现给用户.我的目标是点击上述 ImageView
并更改 drawable 的颜色.
I have an ImageView
, in which I am programmaticly creating drawables and presenting them to the user. My goal is to click on said ImageView
and change the drawable's color.
我将如何处理随机颜色变化位?我目前正在修改 Random()
、Color.argb()
和其他一些东西,但我似乎无法让它工作!
How would I go about the random color changing bit? I am currently tinkering with Random()
, Color.argb()
and a few other things, but I can't seem to get it to work!
推荐答案
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 是什么?是图像、形状、填充...
Though in your case it seems that you want to create a new drawable and assign it to your view. What is actually the drawable in your case? Is it an image, shape, fill...
这篇关于Android:点击时生成随机颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!