我在 drawable 目录和ImageButton中有320张图片,所以当它被点击时,图像必须随机更改,图像名称就像这样file_xyz
,< strong> xyz 是使用此代码随机生成的数字:
rand = new Random(System.currentTimeMillis());
x = rand.nextInt(3 - 0) + 0;
y = rand.nextInt(7 - 0) + 0;
z = rand.nextInt(9 - 0) + 0;
返回&#34;形状_&#34; + x + y + z;
所以这给了我一个字符串,我想用它来改变ImageButton的资源,那么如何应用它并在不同的时间内随机进行更改?
答案 0 :(得分:0)
试试这个:
int resID = getResources().getIdentifier(pDrawableName , "drawable", getPackageName());
imageview.setBackgroundResource(resID);
其中String pDrawableName = file_xyz
是您的图片名称
答案 1 :(得分:0)
如果您首先在drawable文件夹中创建确切的文件名字符串(我将其称为字符串图像),您可以执行以下操作:
ImageButton imageButton = (ImageButton) findViewById(R.id.image_button);
imageButton.setImageResource(R.drawable.image);
其中image_button是您设置为图像按钮ID的任何内容。