如何显示随机图像在Android? [英] How to show random images on Android?

查看:142
本文介绍了如何显示随机图像在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的目录中的号码图像。 我想显示随机图像的机器人。 请任何人都为我提供了一个例子。

I have a number of images in my directory. I want to show random images in ANDROID. Please anyone provide me with an example.

推荐答案

假设你的形象被命名为img1.png,img2.png,等等,它们都位于RES /绘制文件夹。

Assume that your images are named img1.png, img2.png, and so on, and they are located in res/drawable folder.

然后你可以用下面的code随机设置图像中的ImageView

Then you can use the following code to randomly set an image in an ImageView

ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
imgView.setImageResource(id); 

这篇关于如何显示随机图像在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆