Get random images in Android Studio

2019-09-18 12:52发布

问题:

I am trying to create a card game for an android app (for practice) and I was wondering how would I upload random cards into the players hand? I have the GUI implemented and I think I would need something along the lines of a typedarray and

   ((ImageView)findViewById(R.id.textView18)).setImageResource(R.drawable.1c);

Am I moving along the right tracks with this?

回答1:

You can get random images like this:

int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();
imageView.setImageResource(images[rand.nextInt(images.length)]);