简体   繁体   中英

How to set background resource of imageview from a randomized arraylist?

What I want to do is that I'll fill up the 12 stars in this layout from the ArrayList but each in random position, with random star in the drawable to be used.

I have 6 unique stars (blue, green, orange, red, violet, yellow), each to be duplicated for a total of 12.

*function shuffle() was placed inside onCreate.

Layout :

在此处输入图片说明

Codes :

List<Integer> imageViews = new ArrayList<Integer>();
List<Integer> images = new ArrayList<Integer>();


public void viewadd()
{
    imageViews.add(R.id.star1);
    imageViews.add(R.id.star2);
    imageViews.add(R.id.star3);
    imageViews.add(R.id.star4);
    imageViews.add(R.id.star5);
    imageViews.add(R.id.star6);
    imageViews.add(R.id.star7);
    imageViews.add(R.id.star8);
    imageViews.add(R.id.star9);
    imageViews.add(R.id.star10);
    imageViews.add(R.id.star11);
    imageViews.add(R.id.star12);
}



public void imageadd()
{
    images.add(R.drawable.blue);
    images.add(R.drawable.green);
    images.add(R.drawable.orange);
    images.add(R.drawable.red);
    images.add(R.drawable.violet);
    images.add(R.drawable.yellow);
    images.add(R.drawable.blue);
    images.add(R.drawable.green);
    images.add(R.drawable.orange);
    images.add(R.drawable.red);
    images.add(R.drawable.violet);
    images.add(R.drawable.yellow);

}



public void shuffle()
    {
            // TODO Auto-generated method stub

             imageadd();
                viewadd();


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

                      Integer next = rng.nextInt(imageViews.size()) ;
                      if (!generated.contains(next))
                        {
                         generated.add(next);
                         ImageView iv = (ImageView)findViewById(imageViews.get(next));
                         iv.setBackgroundResource(images.get(next));
                         images.remove(next);
                         imageViews.remove(next);
                         break;
                        }

                    }

                }
         }

First create a List of images you want to use,

List<Drawable> listOfDrawable = new ArrayList<Drawable>() ;

Then just fill that list up with the drawables you want to use.

The Java Collections Framework does provide you with a handy method to mix up a collection, like this

Collections.shuffle(listOFDrawalble) Note: the "s" in Collections.

then you got yourself a randomized list of images.

Click HERE to learn more about this utility class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM