Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select from resources randomly (R.drawable.xxxx)

I want to display a random image from the bunch of images i have stored in res/drawable.

The only technique that I know is to access a particular image if you know its resource id. Is there a way to access the images by using the filenames or something else (which can be constructed at runtime)?

I want to randomly select an image at runtime. Any suggestions/ideas appreciated :)

Thanks Chinmay

like image 273
chinmay dhodapkar Avatar asked Aug 16 '10 20:08

chinmay dhodapkar


4 Answers

I realize this is a super old question, but wanted to provide a good solution for any Googlers.

Access resources by name is much less efficient than by id. The whole point of the resource system is so that you don't have to resolve names, and can use ids instead.

What you need to do is utilize the Array Resource type. Follow along with my easy steps! I make random images...Fun!

  1. Create an array resource that includes all of the images you want to choose from. I put this in my res/values/arrays.xml file but it can really go anywhere.

    <array name="loading_images">
        <item>@drawable/bg_loading_1</item>
        <item>@drawable/bg_loading_2</item>
        <item>@drawable/bg_loading_3</item>
        <item>@drawable/bg_loading_5</item>
        <item>@drawable/bg_loading_6</item>
        <item>@drawable/bg_loading_7</item>
        <item>@drawable/bg_loading_8</item>
    </array>
    
  2. Next, get the TypedArray from the Resources and use that to choose a random image.

    TypedArray images = getResources().obtainTypedArray(R.array.loading_images);
    int choice = (int) (Math.random() * images.length());
    mImageView.setImageResource(images.getResourceId(choice, R.drawable.bg_loading_1));
    images.recycle();
    
  3. Profit!

like image 121
rharter Avatar answered Nov 18 '22 20:11

rharter


You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme.

You have to map the name to the identifier using the getIdentifier method of the Resources class.

String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");

The documentation for this method says:

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

This is true but need not be a problem if you are doing it in code that isn't performance sensitive.

Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.

like image 6
Dan Dyer Avatar answered Nov 18 '22 20:11

Dan Dyer


The items in res/drawable are enumerated in the R.drawable class at compile time. You could probably use reflection to get a list of the members of that class, and then select from that list randomly.

like image 2
Cheryl Simon Avatar answered Nov 18 '22 18:11

Cheryl Simon


I can't think of anything you can do at runtime. You could possibly create an array of address integers (since the R.drawable.xxxx is essentially an integer address) and then use java.util.Random to select a random resource address from your array.

like image 1
bporter Avatar answered Nov 18 '22 18:11

bporter