2011-09-19 94 views
3

我使用9圖像視圖就是我想要一套圖像ImageView的隨機,當我點擊刷新按鈕,但我想這樣它的工作對圖像的隨機分配,但它的重複兩個相同的圖像(或)三大的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)]); 
      } 
     } 
    });  

我不想重複,只有一個ImageView的一個形象..

+0

我沒有得到如何使用我的要求。 –

回答

6

,我寫道,你需要一個類似的代碼。檢查這是否有助於你。

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

它的工作不錯..感謝ü.. –

+0

請,這個循環是不是重生,你正在尋找隨機量數字,直到你獲得的所有號碼從1到9!這就像擲骰子,直到你得到1到6的所有數字!有時候,你需要6次嘗試,但大部分時間,你需要10,15或20次嘗試! 您肯定會浪費無用的計算手機的電池!請考慮使用我的第一個解決方案,不要浪費你的應用程序的CPU,你很幸運,你只需要9個隨機數字,想象你需要1.000,你的手機就會死亡。 –

+1

另外,您忘記了「generated.add(next);」在你的循環中!該代碼無法工作!請考慮閱讀這樣的回答:http://stackoverflow.com/questions/4040001/java-creating-random-numbers-with-no-duplicates –

1

也許不是完美的答案來產生隨機數,但我只想洗牌的圖像列表和所產生的圖像設置爲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++; 
} 

作爲替代,您可能還希望使用此代碼隨機播放列表:使用blessenm職務

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); 
     } 
    } 
} 
相關問題