由网友(不撒娇的折耳猫)分享简介:我有我的目录中的号码图像。我想显示随机图像的机器人。请任何人都为我提供了一个例子。I have a number of images in my directory.I want to show random images in ANDROID.Please anyone provide me with an...
我有我的目录中的号码图像。 我想显示随机图像的机器人。 请任何人都为我提供了一个例子。
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);
相关推荐
最新文章