Reputation: 6128
Guys i am developing an puzzle game where in one part i want to cut the image to 15 pieces which fills the entire screen and also i need to arrange those pieces one below the other (ontouch screen).
my image dimention is 414*310 and i got the code i am not sure how to go with it please help.
i have my image in drawable folder.
public Bitmap[] splitBitmap(Bitmap picture)
{
Bitmap scaledBitmap = Bitmap.createScaledBitmap(picture, 240, 240, true);
Bitmap[] imgs = new Bitmap[9];
imgs[0] = Bitmap.createBitmap(scaledBitmap, 0, 0, 80 , 80);
imgs[1] = Bitmap.createBitmap(scaledBitmap, 80, 0, 80, 80);
imgs[2] = Bitmap.createBitmap(scaledBitmap,160, 0, 80,80);
imgs[3] = Bitmap.createBitmap(scaledBitmap, 0, 80, 80, 80);
imgs[4] = Bitmap.createBitmap(scaledBitmap, 80, 80, 80,80);
imgs[5] = Bitmap.createBitmap(scaledBitmap, 160, 80,80,80);
imgs[6] = Bitmap.createBitmap(scaledBitmap, 0, 160, 80,80);
imgs[7] = Bitmap.createBitmap(scaledBitmap, 80, 160,80,80);
imgs[8] = Bitmap.createBitmap(scaledBitmap, 160,160,80,80);
return imgs;
}
i am working on android 2.1
Upvotes: 3
Views: 4214
Reputation: 13588
So, you first need to have 9(to cut it into 9 pieces) imageviews in your xml. Once you read your image from drawable convert it to bitmap. May be this method can be used to cut it into pieces:
public Bitmap[] createBitmaps(Bitmap source){
Bitmap[] bmp=new Bitmap[];
int k=0;
int width=source.getWidth();
int height=source.getHeight();
for(i=0;i<3;i++){
for(j=0;j<3;j++){
bmp[k]=Bitmap.createBitmap(source,(width*j)/3,(i*height)/3,width/3,height/3);
}
}
return bmp;
}
After creating bitmap array, Set those bitmaps to imageviews using some random numbers.
Upvotes: 5