每个ImageView在使用随机时如何显示不同的图片? Android Studio

时间:2015-09-28 20:17:50

标签: java android image android-activity imageview

我随机设置了4个图像,但是当我测试它时,结果显示相同的图片。我希望每个图像视图都能显示与其他图像不同的图像。

这是我的代码:

UIFocusUpdateContext

4 个答案:

答案 0 :(得分:2)

查看代码,用相同的图像替换所有4张照片(例如" img1"或" img2"或" img3"或&#34 ; IMG4&#34)

如果你想用不同的图像替换4 pic,只需调用:

int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
每次在将资源ID分配给ImageView之前

创建一个函数,例如:

private int getRandomImage() {
    int rndInt = rand.nextInt(4) + 1;
    String imgName = "img" + rndInt;
    return getResources().getIdentifier(imgName, "drawable", getPackageName());
}

并调用每个ImageView

pic1.setImageResource(getRandomImage());
pic2.setImageResource(getRandomImage());
pic3.setImageResource(getRandomImage());
pic4.setImageResource(getRandomImage());

修改

如果你想拥有4个不同的图像,可以用以下方法更改getRandomImage():

List<Integer> listImageUsed = new ArrayList<Integer>();

private int getRandomImage() {
    int rndInt;
    do {
        rndInt = rand.nextInt(4) + 1;
    } while (!listImageUsed.contains(rndInt));

    listImageUsed.add(rndInt);

    String imgName = "img" + rndInt;
    return getResources().getIdentifier(imgName, "drawable", getPackageName());
}

答案 1 :(得分:2)

这是我的回答,其他的尝试只显示4个不同的图像。我的解决方案提供的图像是随机的,不相同:

            final ImageButton imageButton=(ImageButton) findViewById(R.id.imageView);
                    imageButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            imageButton.setVisibility(View.INVISIBLE);
                            pic1 = (ImageView) findViewById(R.id.imageView2);
                            pic2 = (ImageView) findViewById(R.id.imageView3);
                            pic3 = (ImageView) findViewById(R.id.imageView4);
                            pic4 = (ImageView) findViewById(R.id.imageView5);

                            ArrayList<Integer> imageIds= new ArrayList<>(Arrays.asList(1, 2, 3, 4))
                            Collections.shuffle(imageIds);

                            pic1.setImageResource(getResources().getIdentifier("img" + imageIds[0], "drawable", getPackageName()));
                            pic2.setImageResource(getResources().getIdentifier("img" + imageIds[1], "drawable", getPackageName()));
                            pic3.setImageResource(getResources().getIdentifier("img" + imageIds[2], "drawable", getPackageName()));
                            pic4.setImageResource(getResources().getIdentifier("img" + imageIds[3], "drawable", getPackageName()));

                        }
                    });

此外,您不必更改大部分代码以使其正常工作;) 对不起,我把所有东西都装在一行。但是我在德国真的很累,现在快到午夜了。

答案 2 :(得分:0)

这是相同的图像因为&#34; id&#34;只设置一次,以获得差异。您必须生成更多随机数。你需要调用rand.nextInt(4)四次。在每个&#34; .setImageResource(id)&#34;

之后

只是为了给你一个想法。

Random rand = new Random();
int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
pic1.setImageResource(id);
//Here you re-generate a random number
rndInt = rand.nextInt(4) + 1;
imgName = "img" + rndInt;
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
pic2.setImageResource(id);

答案 3 :(得分:0)

     final ImageButton imageButton=(ImageButton) findViewById(R.id.imageView);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    imageButton.setVisibility(View.INVISIBLE);
                    pic1 = (ImageView) findViewById(R.id.imageView2);
                    pic2 = (ImageView) findViewById(R.id.imageView3);
                    pic3 = (ImageView) findViewById(R.id.imageView4);
                    pic4 = (ImageView) findViewById(R.id.imageView5);


                  setRandomImage(pic1);
                  setRandomImage(pic2);
                  setRandomImage(pic3);
                  setRandomImage(pic4);
                }
            });
private void setRandomImage(ImageView imgView)
{
   Random rand = new Random();
   int rndInt = rand.nextInt(4) + 1;
   String imgName = "img" + rndInt;
   int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
   imgView.setImageResource(id);
}