在Android Studio中的运行时随机应用图像

时间:2018-11-01 14:04:48

标签: android

我已经为4个图像按钮编写了代码,并且在运行时将为每个图像分配随机图像,以使2个图像具有相同的图像,另外2个图像具有相同的图像。 现在,IB1和IB3将具有相同的图像,IB2和IB4将具有相同的图像。 IB1- ImageButton1(IB1是ID)。 我想要的是..应该随机应用图像..就像一次I1和I2可能具有相同的图像..而下一次I1和I4可能具有相同的图像。

public class MainActivity extends Activity {
    final Random rnd = new Random(); 

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        final ImageButton img1 = (ImageButton) findViewById(R.id.I1);
        // I have 3 images named img_0 to img_2, so...
        final String str1 = "img_" + rnd.nextInt(20);

        img1.setImageDrawable(
           getResources().getDrawable(getResourceID(str1, "drawable", getApplicationContext()))
        );

       final ImageButton img2 = (ImageButton) findViewById(R.id.I2);

       final String str2 = "img_" + rnd.nextInt(20);
       img2.setImageDrawable(
            getResources().getDrawable(getResourceID(str2, "drawable", getApplicationContext()))
       );

       final ImageButton img3 = (ImageButton) findViewById(R.id.I3);

       //final String str3 = "img_" + rnd.nextInt(20);
       img3.setImageDrawable(
           getResources().getDrawable(getResourceID(str1, "drawable", getApplicationContext()))
       );

       final ImageButton img4 = (ImageButton) findViewById(R.id.I4);

       //final String str4 = "img_" + rnd.nextInt(20);
       img4.setImageDrawable(
           getResources().getDrawable(getResourceID(str2, "drawable", getApplicationContext()))
       );
   }

   protected final static int getResourceID(final String resName, final String resType, final Context ctx) {
       final int ResourceID = ctx.getResources().getIdentifier(resName, resType, ctx.getApplicationInfo().packageName);
       if (ResourceID == 0) {
           throw new IllegalArgumentException("No resource string found with name " + resName);
       } else {
           return ResourceID;
       }
   }

}

2 个答案:

答案 0 :(得分:0)

一个简单的解决方案是首先选择任意两个图像。您已经有相应的代码。拥有两个图像后,选择任意两个按钮。一个简单的随机数选择将为您提供两个按钮(0-3个随机数选择)。然后,您只需将图像设置为这些按钮即可。

编辑

下面是一些代码,展示了如何实现这一目标

final String imageResource1 = "img_" + rand.nextInt(20);
final String imageResource2 = "img_" + rand.nextInt(20);

List<ImageButton> imageButtons = new ArrayList<>();
imageButtons.add(IB1);
imageButtons.add(IB2);
imageButtons.add(IB3);
imageButtons.add(IB4);

//Now we select the buttons which will use the first image
int imageButtonIndex = rand.nextInt(4);
imageButtons.get(imageButtonIndex).setImageResource(
    getResources().getDrawable(getResourceID(str1, "drawable", getApplicationContext())
);
imageButtons.remove(imageButtonIndex);

imageButtonIndex = rand.nextInt(3);
imageButtons.get(imageButtonIndex).setImageResource(
    getResources().getDrawable(getResourceID(str1, "drawable", getApplicationContext())
);

//Now we just fetch and set the image 2 to the remaining buttons
imageButtons.get(0).setImageResource(
    getResources().getDrawable(getResourceID(str2, "drawable", getApplicationContext())
);

imageButtons.remove(0);

imageButtons.get(0).setImageResource(
    getResources().getDrawable(getResourceID(str2, "drawable", getApplicationContext())
);

有很多可以改进的地方,但这是实现此目的的直接方法

答案 1 :(得分:0)

1. If there are 20 buttons. Button IDs are IB0, IB1, ... IB19.
2. If there are 10 Images. Image Names are img_0, img_1, ... img_9.

这个小程序将为10个图像分配20个按钮,以便每个图像使用两次。使用Button而不是ImageButton,以便在xml级别不需要图像。

ArrayList<String> image_list = new ArrayList<>();
    for (int b = 0; b < 2; b++) {for (int a = 0; a < 10; a++) {
            image_list.add("img_" + Integer.toString(a)); }}
    Collections.shuffle(image_list);

for (int i=0; i<20; i++){
    int btnId = getResources().getIdentifier("IB" + i, "id", this.getPackageName());
    Button btn = findViewById(btnId);
    int drawableId = getResources().getIdentifier(image_list.get(i), "drawable", this.getPackageName());
    btn.setBackgroundResource(drawableId);
    }

The end result will be 20 buttons with Images. 10 'PAIRS' in total, as in a 'match the cards' game.
It will be in random order each time you run it.
This is the smallest program I could come up with. Hope this helps. 

感谢以下人员的帮助:堆栈溢出贡献者“ navylover”。