【问题标题】:How to set random images to ImageView's?如何将随机图像设置为 ImageView?
【发布时间】:2011-11-20 04:40:04
【问题描述】:

我正在使用 9 个图像视图,当我单击刷新按钮时,我想将图像随机设置为 imageview,但我尝试这样它适用于随机分配图像,但它在两个(或)三个 imageview 中重复相同的图像一次。我的代码中的问题在哪里..

 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)]);
            }
        }
    });      

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

【问题讨论】:

  • 我不知道如何在我的要求中使用。

标签: android image random imageview


【解决方案1】:

您可能想参考这篇文章。它显示了一种生成无重复随机数的方法 Creating random numbers with no duplicates

【讨论】:

    【解决方案2】:

    也许不是完美的答案,但我只是打乱图像列表并将生成的图像设置为图像视图。

    这将避免生成随机数,这当然会产生重复(如果你掷骰子 6 次,你不会有随机顺序的数字 1,2,3,4,5,6,你会多次获得相同的数字。)

    请检查所有内容,包括“i”,因为我不在电脑前。

    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);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用 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;
                       }
                     }
                  }
                  }
              });
      

      【讨论】:

      • 拜托,这个循环不是天生的,你正在寻找随机数,直到你得到从 1 到 9 的所有数字!这就像掷骰子,直到你得到从 1 到 6 的所有数字!有时,您需要 6 次尝试,但大多数时候,您需要 10、15 或 20 次尝试!您肯定会将手机电池浪费在无用的计算中!请考虑使用我的第一个解决方案,不要在您的应用程序上浪费 CPU,您很幸运,您只需要 9 个随机数,假设您需要 1.000,您的手机就会死机。
      • 另外,你忘记了“generated.add(next);”在你的循环中!代码无法运行!请考虑阅读此答案:stackoverflow.com/questions/4040001/…
      • 你是对的 +1,让我编辑我的答案。 @raj 添加 profete 建议的缺失行。
      • 如何检测选择/使用了哪些可绘制对象?是否可以标记它们或将 ClipData 放入每个特定的可绘制对象中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多