gpt4 book ai didi

java - android 选择随机图像?

转载 作者:行者123 更新时间:2023-11-30 04:32:00 25 4
gpt4 key购买 nike

我如何从 2 张随机图片中加载一张?

我试过这段代码

final Random rgenerator = new Random();
Integer [] mImageIds =
{
R.drawable.mantrans,
R.drawable.womentrans,
};
Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
gBall1 = BitmapFactory.decodeResource(getResources(), q);

但是没用...

请帮忙

最佳答案

这对你有用..

  /////*****Random numbers with out repetation*****//////
final int[] imageViews = {
R.id.imgview11, R.id.imgview12, R.id.imgview13 };

final int[] images = {
R.drawable.i1, R.drawable.i2, R.drawable.i3 };

Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>(maxNoOfImages);
for (int i = 0; i < maxNoOfImages; i++) {
while(true) {
Integer next = rng.nextInt(maxNoOfImages);

if (!generated.contains(next)) {
ImageView iv = (ImageView)findViewById(imageViews[i]);
iv.setImageResource(images[next]);

generated.add(next);
break;
}
}
}

关于java - android 选择随机图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com