-1
public class GameView extends View {
    private Bitmap bmp1;
    private Bitmap bmp2;
    private Bitmap bmp3;
    private Bitmap bmp4;
    private Bitmap bmp5;
    private Bitmap bmp6;
    public GameView(Context context) {
        super(context);

        bmp1 = BitmapFactory.decodeResource(getResources(),R.drawable.candy1);
        bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.candy2);
        bmp3 = BitmapFactory.decodeResource(getResources(),R.drawable.candy3);
        bmp4 = BitmapFactory.decodeResource(getResources(),R.drawable.candy4);
        bmp5 = BitmapFactory.decodeResource(getResources(),R.drawable.candy5);
        bmp6 = BitmapFactory.decodeResource(getResources(),R.drawable.candy6);

//here my 6 images i made them in this way to control thier positions x,y

}
@Override
protected void onDraw(Canvas canvas){
    Bitmap[] images = {bmp1 , bmp2 , bmp3 , bmp4 , bmp5 , bmp6};
    canvas.drawColor(Color.WHITE);
    for (int i = 0; i<306; i+=61)
    { 
        canvas.drawBitmap(bmp1, i, 0, null);
        canvas.drawBitmap(bmp2, i, 61, null);
        canvas.drawBitmap(bmp3, i, 122, null);
        canvas.drawBitmap(bmp4, i, 183, null);
        canvas.drawBitmap(bmp5, i, 244, null);
        canvas.drawBitmap(bmp6, i, 305, null);

//how to use collections.shuffle in the loop to insert the 6 images randomly , and insert the images with these positions , i made array of bitmap but how to use it in collections.shuffle so it can display my imgaes randomly with these postions ?

}
}   
}
Reta Adam
  • 5
  • 1
  • 1
    Please add some explanation instead of just posting your code (even though there are questions as comments). – Roope Hakulinen Dec 20 '14 at 20:53
  • hmmmm if i understand well. Just shuffle another array with numbers from 0-5. that's it. – Saif Hamed Dec 20 '14 at 20:59
  • i want to use Collections.shuffle in the images so it can insert them randomly at the same time i want all the images to be displayed with the positions in the loop – Reta Adam Dec 20 '14 at 21:00
  • This shows the basic pattern: http://www.tutorialspoint.com/java/util/collections_shuffle.htm. You need to put the Bitmaps into a List. You need to call the shuffle command in `onDraw()`. Your loop does not look correct. Pull the images from the List in the loop and at the same time assign coordinates as part of the `drawBitmap` command -- only one line inside the loop, in other words. – JASON G PETERSON Dec 20 '14 at 21:02

1 Answers1

0

Create field List and in constructor add bitmaps to it. For example:

this.bitmaps = new ArrayList<Bitmap>();
//add bitmaps to that ArrayList
this.bitmaps.add(BitmapFactory.decodeResource(getResources(),R.drawable.candy1);
...

//now shuffle that list
Collections.shuffle(this.bitmaps, new Random());

Now draw them in onDraw method:

for (int x = 0; x < 6; x++){
    //Don't repeat yourself
    for(int y = 0; y < 6; y++){
        canvas.drawBitmap(this.bitmaps.get(y), x * 61, y * 61, 0, null);
    }
}

That should do the trick.

David Novák
  • 1,455
  • 2
  • 18
  • 30