Java 使用Random类获取随机颜色?
我的应用程序要求listview项目的颜色为浅色,但不是透明色。我得到了随机的颜色,但它经常是很暗的。我不知道在哪个范围内我只能得到浅色。随机给定的范围如下所示:Java 使用Random类获取随机颜色?,java,android,Java,Android,我的应用程序要求listview项目的颜色为浅色,但不是透明色。我得到了随机的颜色,但它经常是很暗的。我不知道在哪个范围内我只能得到浅色。随机给定的范围如下所示: Random rndm = new Random(); int color = Color.argb(255, rndm.nextInt(256), rndm.nextInt(256), rndm.nextInt(256)); RelativeLayout rl=(RelativeLayout)rowView.findViewB
Random rndm = new Random();
int color = Color.argb(255, rndm.nextInt(256), rndm.nextInt(256), rndm.nextInt(256));
RelativeLayout rl=(RelativeLayout)rowView.findViewById(R.id.front);
rl.setBackgroundColor(color);
我向你指出了一个想法,我将在这里解释:
Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
Bitmap bitmap = new Bitmap(context);
bitmap.eraseColor(color);
Palette.from(bitmap).getLightVibrantColor();
看到了吗?我正在创建一个虚拟的
位图
,用随机生成的颜色填充它,然后使用调色板
API来获得“明亮的”颜色。通常需要一个大于128的RGB值来获得明亮的颜色,因此拾取3个随机值,并检查其中一个是否大于128就足够了。该值越大,它将越亮
您可以检查平均值是否大于128,这样可以带来良好的结果
public int getRandomColor() {
Random rndm = new Random();
int r = rndm.nextInt(256);
int g = rndm.nextInt(256);
int b = rndm.nextInt(256);
int adjust = 0;
if( (r + g + b)/3 < 128)
int adjust = (128 - (r + g + b)/3)/3;
r += adjust;
g += adjust;
b += adjust;
return Color.argb(255, r, g, b);
}
public int getRandomColor(){
随机rndm=新随机();
int r=rndm.nextInt(256);
int g=rndm.nextInt(256);
int b=rndm.nextInt(256);
int调整=0;
如果((r+g+b)/3<128)
整数调整=(128-(r+g+b)/3)/3;
r+=调整;
g+=调整;
b+=调整;
返回颜色.argb(255,r,g,b);
}
您需要限制选择的颜色空间。现在你使用的是整个RGB光谱,其中一半是深色。因此,我建议您为所有通道设置一个基线颜色范围(通过实验或查看您最喜欢的图形程序的颜色选择器),然后将该范围随机设置在该基线之上
final int basec = 128; //baseline would be 50% gray
Random rndm = new Random();
int color = Color.argb(255, basec + rndm.nextInt(256-basec), basec + rndm.nextInt(256-basec), basec + rndm.nextInt(256-basec));
RelativeLayout rl=(RelativeLayout)rowView.findViewById(R.id.front);
rl.setBackgroundColor(color);
要获得明亮的随机颜色,您需要使每个通道具有一些最小值 代码如下:
// minimumBrightnessRatio between 0 and 1. 1 = all white, 0 = any possible color. Best results would be 0.5.
public int getRandomColor(float minimumBrightnessRatio)
{
Random rndm = new Random();
final int minimumColorValue = (int)(255f * minimumBrightnessRatio);
final int variableColorValue = 255 - minimumColorValue;
final int r = minimumColorValue + rndm.nextInt(variableColorValue);
final int g = minimumColorValue + rndm.nextInt(variableColorValue);
final int b = minimumColorValue + rndm.nextInt(variableColorValue);
return Color.argb(255, r, g, b);
}
在处理光的强度时(在您的情况下,您需要光的颜色),
HSI模型
非常方便。您可以随机生成介于0.8到1之间的强度,并随机化色调和饱和度。然后将其转换为RGB 你想要浅色。这意味着RGB组件值必须介于128和255之间
由于Random.nextInt(int max)
返回介于0和max exclusive之间的int,因此您可能需要尝试以下操作:
Random rnd = new Random();
int r = rnd.nextInt(128) + 128; // 128 ... 255
int g = rnd.nextInt(128) + 128; // 128 ... 255
int b = rnd.nextInt(128) + 128; // 128 ... 255
Color clr = Color.rgb(r, g, b);
若你们已经生成了一个位图,那个么调色板应该是一个解决方案:我并没有生成位图,正如你们所看到的,我只是将BackgroundColor设置为相对布局。你们可以。。。这只是一个想法。看看我的答案,试试看。事实上,在128和255之间会包含较浅的颜色(只是为了解释)。你的方法会返回我想要的,返回我22961645335561和8141674,而Bowska返回129583791036130811232046。这些结果适用于三个项目。非常感谢。看来你已经得到了答案。顺便说一句,对于你发布的每一个问题,关注一个特定的用户并不是一个好主意。我没有所有的答案。尤其是这件事,我无法回答。?但这个你可以请。德尔格伦接近返回我22961645335561和8141674,而你返回129583791036130811232046。这些结果适用于三个项目@鲍斯卡。非常感谢。谢谢@shkschneider。我明白了@shkschneiderSir,我想让用户界面像这样,我无法发布答案,它不符合我认为的标准。我只是想制作这个应用的主页。滚动效果。请引导我。真的很紧急。