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);
//在这里我的6张图片我以这种方式制作它们来控制它们的位置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);
//如何在循环中使用collections.shuffle随机插入6个图像,并插入具有这些位置的图像,我制作位图数组,但如何在collections.shuffle中使用它,以便它可以随机显示我的imgaes有这些位置?
}
}
}
答案 0 :(得分:0)
创建字段列表并在构造函数中添加位图。例如:
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());
现在用onDraw
方法绘制它们:
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);
}
}
这应该可以解决问题。