random genaration of image from drawable folder in

2019-02-11 04:54发布

问题:

Android version:4.2
I am developing an android App. I need to generate images from drawable folder randomly. In my drawable I have 45 images with different names. My xml code is:

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

I have tried with this code:

ImageView img=(ImageView)findViewById(R.id.imageView1);
Random rand = new Random();
int rndInt = rand.nextInt(52) + 1;
String drawableName = "photo"+ rndInt;

int resID = getResources().getIdentifier(drawableName, "drawable",  getPackageName());
img.setImageResource(resID);

But with this code I need to change my image names to photo1, photo2, ... and I don't want to do it.

Any suggestion on how to implement it? Thank you.

回答1:

One way is to create an array with required image's ids. And take random one from that array. That approach is explained in other answers.

Another way is to create file random_images_array.xml in values folder of your project and fill it like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="apptour">
        <item>@drawable/image_1</item>
        <item>@drawable/photo_2</item>
        <item>@drawable/picture_4</item>
    </array>

</resources>

And then you can take random image from that xml array:

final TypedArray imgs = getResources().obtainTypedArray(R.array.random_images_array);
final Random rand = new Random();
final int rndInt = rand.nextInt(imgs.length());
final int resID = imgs.getResourceId(rndInt, 0);

Third method is to take random field from R.drawable class:

final Class drawableClass = R.drawable.class;
final Field[] fields = drawableClass.getFields();

final Random rand = new Random();
int rndInt = rand.nextInt(fields.length);
try {
    int resID = fields[rndInt].getInt(drawableClass);
    img.setImageResource(resID);
} catch (Exception e) {
    e.printStackTrace();
}


回答2:

How about

long[] res = {R.drawable.image1, R.drawable.image2};

or

int[] res = {R.drawable.image1, R.drawable.image2};

and

 int rndInt = rand.nextInt(res .length);



img.setImageDrawable(getResources().getDrawable(res[rndInt]));


回答3:

Be specific about your question - what do you actually want to do?

if you want to show images in random order than this would be best

        int resId[]={R.drawable.p1,R.drawable.p2,R.drawable.p2};
         Random rand = new Random();
         int index = rand.nextInt((resId.length- 1) - 0 + 1) + 0;

         imgView.setImageResource(resId[index]);

If you want the absolute file path of image to rename it, see this article for details.



回答4:

ImageView img=(ImageView)findViewById(R.id.imageView1);
String[] imageArray = {"Image1", "Image2", etc..};
Random rand = new Random();

int rndInt = rand.nextInt(52) + 1;
int resID = getResources().getIdentifier(imageArray[rand], "drawable",  getPackageName());
img.setImageResource(resID);


回答5:

You have to see this question or answer also:-

Randomize string from resources android

but you have to replace

textview.setText()

to

img.setImageResource(ran.nextInt(trivias.length)]);`