如何根据整数变量的值设置 imageview 资源。 Int= X //R.drawable.imageX

How to set imageview resource based on value of integer variable. Int= X //R.drawable.imageX

我想知道如何动态设置imageview的来源。我有 150 张图像(d1、d2、d3.......、d150),我有整数变量 "DungeonLevel",随着时间的推移从 1 变为 150。我不知道如何写它。当我试图找到它时,它不一样或者我没有找到它。很抱歉,如果您认为它重复了。

int X;
String imageName = "image" + X;
String uriOfImage = "@drawable/" + imageName;
int imageResource = getResources().getIdentifier(uriOfImage, null, getPackageName());
Drawable resourceDrawable = getResources().getDrawable(imageResource);
imageView.setImageDrawable(resourceDrawable);

这里的X代表你的DungeonLevel介于1150.[=13=之间]