Android: generate unrepeating random images
I'm having trouble in making unrepeating random images on my Android App.
Let's say on my App I have this TOP 3 Sections, and I have 13 pictures. I need a method to generate 3 random images from the 13 pics on @drawable I have, but the 3 pictures should be different from one another.
For example: it can't be img1,img1,img2 or img1,img1,img1 on the TOP 3 section but it should be img1,img2,img3.
So far, I have this method in hand:
final Button imgView = (Button)findViewById(R.id.top1);
Random rand = new Random();
int rndInt = rand.nextInt(13) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
On my layout, I specified one of the image ID on the TOP 3 section as top1. I still have the id top2, and top3 for the TOP 3 section. The above code will look up to my drawable images, which have the names 'img1.jpg', 'img2.jpg', 'img3.jpg' , 'img4.jpg', etc.
I'm open to any kind of solution. If somebody could improvise my code so 3 pictures won't be the same with each other that would be perfect. But if any of you can offer 开发者_JAVA技巧a better solution with a new method, I'm open for it.
Thank you.
I'd suggest you to store your image ids in List
, use shuffle
and retrieve the first element by removing.
//the initial list of images
final List<String> images = new ArrayList<String>();
for (int i=1; i<=13; i++){
images.add("img"+i);
}
//the method that retrieves random value
Collections.shuffle(images, new Random());
String randomImage = images.remove(0);
EDIT:
//this code should be inoked on every image add
final Button imgView = (Button)findViewById(R.id.top1);
Collections.shuffle(images, new Random());
String imgName = images.remove(0);
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
The initialization of list should be performed once, outside of image add method (somewhere in onCreate
probably.
Just an idea:
You can store rndInt
in a map and check if
map.containsKey(rndInt)
, get another random id otherwise (and check again if the new id is in map) , else
keep going with the normal flow
u can try this to get random images in view...this is working for me.....
final int[] imageViews = {
R.id.imageView2, R.id.imageView10, R.id.imageView3,
R.id.imageView4, R.id.imageView5, R.id.imageView6, R.id.imageView8 };
int[] photos={R.drawable.aa, R.drawable.pp
,R.drawable.ee,
R.drawable.pp_blue,R.drawable.ll};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for randomizing the view
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 5; i++)
{
while(true)
{
Integer next = rng.nextInt(5) ;
if (!generated.contains(next))
{
generated.add(next);
ImageView iv = (ImageView)findViewById(imageViews[i]);
iv.setImageResource(photos[next]);
break;
}
}
} /// end of random
}
精彩评论