如何将随机图像设置为ImageView's?[英] How to set random images to ImageView's?

本文是小编为大家收集整理的关于如何将随机图像设置为ImageView's?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用9个图像视图,当我单击"刷新"按钮时,我希望将图像设置为随机映像,但是我尝试过这样的图像来随机分配图像,但是它在两个(或)三个图像视图中重复相同的图像.时间.我的代码中的问题在哪里.

 final int[] imageViews = {
            R.id.imgview11, R.id.imgview12, R.id.imgview13, 
            R.id.imgview21, R.id.imgview22, R.id.imgview23, 
            R.id.imgview31, R.id.imgview32, R.id.imgview33 };

    final int[] images = {
            R.drawable.i1, R.drawable.i2, R.drawable.i3, 
            R.drawable.i4, R.drawable.i5, R.drawable.i6, 
            R.drawable.i7, R.drawable.i8, R.drawable.empty };

    final ImageButton shuffle = (ImageButton) findViewById(R.id.new_puzzle); 
    shuffle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {        
            Random generator = new Random();
            //int n = 9;
            //n = generator.nextInt(n);
            //Random random = new Random(System.currentTimeMillis());
            for(int v : imageViews) {
                ImageView iv = (ImageView)findViewById(v);
                iv.setImageResource(images[generator.nextInt(images.length - 1)]);
            }
        }
    });      

我不想重复,一个图像仅用于一个图像.

推荐答案

使用Blessenm的帖子,我编写了您需要的类似代码.检查这是否对您有帮助.

shuffle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) { 

            Random rng = new Random(); 
            List<Integer> generated = new ArrayList<Integer>();
            for (int i = 0; i < 9; i++)
            {
              while(true)
              {
                 Integer next = rng.nextInt(9) ;
                 if (!generated.contains(next))
                 {
                    generated.add(next);
                    ImageView iv = (ImageView)findViewById(imageViews[i]);
                    iv.setImageResource(images[next]);
                    break;
                 }
               }
            }
            }
        });

其他推荐答案

也许不是完美的答案,但是我只会将图像列表和将结果图像设置为ImageView.

这将避免生成当然会创建重复的随机数(如果您投掷骰子6次,您将不会有1,2,3,4,5,6的数字随机顺序,您将获得多个时间相同的数字.)

请检查所有内容,包括"我",因为我不在计算机前.

List<int> list = Arrays.asList(images);
// Here we just simply used the shuffle method of Collections class
// to shuffle out defined array.
Collections.shuffle(list);

int i=0;
// Run the code again and again, then you'll see how simple we do shuffling
for (int picture: list) {
    ImageView iv = (ImageView)findViewById(imageViews[i]);
    iv.setImageResource(picture);
    i++;
}

作为替代方案,您可能还想用此代码将列表改组:

public class ShuffleArray {
    public static void shuffleArray(int[] a) {
        int n = a.length;
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < n; i++) {
            int change = i + random.nextInt(n - i);
            swap(a, i, change);
        }
    }

    private static void swap(int[] a, int i, int change) {
        int helper = a[i];
        a[i] = a[change];
        a[change] = helper;
    }

    public static void main(String[] args) {
        int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        shuffleArray(a);
        for (int i : a) {
            System.out.println(i);
        }
    }
}

其他推荐答案

您可能需要参考此帖子.它显示了一种生成随机数的方法,而无需重复 创建随机数,没有重复

本文地址:https://www.itbaoku.cn/post/102673.html

问题描述

i am using 9 image view's i want set images to imageview randomly , when I click on refresh button, but I tried like this it's working for random allocation of images but it's repeating the same image in two (or) three imageview's at a time. where is the problem in my code..

 final int[] imageViews = {
            R.id.imgview11, R.id.imgview12, R.id.imgview13, 
            R.id.imgview21, R.id.imgview22, R.id.imgview23, 
            R.id.imgview31, R.id.imgview32, R.id.imgview33 };

    final int[] images = {
            R.drawable.i1, R.drawable.i2, R.drawable.i3, 
            R.drawable.i4, R.drawable.i5, R.drawable.i6, 
            R.drawable.i7, R.drawable.i8, R.drawable.empty };

    final ImageButton shuffle = (ImageButton) findViewById(R.id.new_puzzle); 
    shuffle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {        
            Random generator = new Random();
            //int n = 9;
            //n = generator.nextInt(n);
            //Random random = new Random(System.currentTimeMillis());
            for(int v : imageViews) {
                ImageView iv = (ImageView)findViewById(v);
                iv.setImageResource(images[generator.nextInt(images.length - 1)]);
            }
        }
    });      

i don't want repeat, one image for one imageview only..

推荐答案

using the post of blessenm ,i wrote a similar code that you need. check if this helps you.

shuffle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) { 

            Random rng = new Random(); 
            List<Integer> generated = new ArrayList<Integer>();
            for (int i = 0; i < 9; i++)
            {
              while(true)
              {
                 Integer next = rng.nextInt(9) ;
                 if (!generated.contains(next))
                 {
                    generated.add(next);
                    ImageView iv = (ImageView)findViewById(imageViews[i]);
                    iv.setImageResource(images[next]);
                    break;
                 }
               }
            }
            }
        });

其他推荐答案

Maybe not the perfect answer, but I would just shuffle the images list and the set the resulting image to the imageview.

This will avoid having to generate random numbers that will of course create duplicate (If you throw a dice 6 times, you won't have the numbers 1,2,3,4,5,6 in random order, you will get multiple time the same number.)

Please check everything including the 'i' as I am not in front of my computer.

List<int> list = Arrays.asList(images);
// Here we just simply used the shuffle method of Collections class
// to shuffle out defined array.
Collections.shuffle(list);

int i=0;
// Run the code again and again, then you'll see how simple we do shuffling
for (int picture: list) {
    ImageView iv = (ImageView)findViewById(imageViews[i]);
    iv.setImageResource(picture);
    i++;
}

as an alternative, you may also want to shuffle your list with this code:

public class ShuffleArray {
    public static void shuffleArray(int[] a) {
        int n = a.length;
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < n; i++) {
            int change = i + random.nextInt(n - i);
            swap(a, i, change);
        }
    }

    private static void swap(int[] a, int i, int change) {
        int helper = a[i];
        a[i] = a[change];
        a[change] = helper;
    }

    public static void main(String[] args) {
        int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        shuffleArray(a);
        for (int i : a) {
            System.out.println(i);
        }
    }
}

其他推荐答案

You might want to refer to this post. It shows a method to generate random numbers without duplicates Creating random numbers with no duplicates