自动改变的ImageButton可绘制

问题描述 投票:-3回答:2

我在抽拉目录和320个的ImageButton图像,以便它的点击时,图像必须被随机地改变时,图像名称是这样file_xyz中,XYZ是数字使用此代码随机生成的每一个:

rand = new Random(System.currentTimeMillis());
    x = rand.nextInt(3 - 0) + 0;
    y = rand.nextInt(7 - 0) + 0;
    z = rand.nextInt(9 - 0) + 0;

返回 “shape_” + X + Y + Z;

所以这个给我,我想用它来改变的ImageButton的资源字符串,所以如何应用此,使随机变化在分开的时间?

android random android-imagebutton
2个回答
1
投票

尝试这个:

   int resID = getResources().getIdentifier(pDrawableName , "drawable", getPackageName());    
    imageview.setBackgroundResource(resID);

其中String pDrawableName = file_xyz是你的形象的名字


0
投票

如果先创建确切的文件名字符串作为绘制文件夹(我称之为列图像),你可以做到以下几点:

ImageButton imageButton = (ImageButton) findViewById(R.id.image_button);
imageButton.setImageResource(R.drawable.image);

其中image_button是您所设置的ID为您的图像按钮。

© www.soinside.com 2019 - 2024. All rights reserved.