Reputation: 2426
How to divide the single image into 4 equal parts when user clicks on the button.
Upvotes: 1
Views: 3844
Reputation: 2292
This is the method to divide a bitmap into 4 equal parts. Just pass a bitmap as a parameter to this method.
public Bitmap[] splitBitmap(Bitmap picture)
{
Bitmap[] imgs = new Bitmap[4];
imgs[0] = Bitmap.createBitmap(picture, 0, 0, picture.getWidth()/2 , picture.getHeight()/2);
imgs[1] = Bitmap.createBitmap(picture, picture.getWidth()/2, 0, picture.getWidth()/2, picture.getHeight()/2);
imgs[2] = Bitmap.createBitmap(picture,0, picture.getHeight()/2, picture.getWidth()/2,picture.getHeight()/2);
imgs[3] = Bitmap.createBitmap(picture, picture.getWidth()/2, picture.getHeight()/2, picture.getWidth()/2, picture.getHeight()/2);
return imgs;
}
Upvotes: 0
Reputation: 4589
I found the below code that work great. It divide image into 9 parts. By using it you can divide image into 4 parts as well.
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;
}
The function takes the original bitmap as a parameter, then using the Bitmap.createScaledBitmap(picture, 240, 240, true); i created a scaled image of size 240 x 240 to split the image into equal pieces, i have created a 3 x 3 by grid, in which each image’s size is of 80 x 80. This can be changed as per your needs, but the width should be kept to 240, because all the normal android phone screens are 240 dip wide.
All the bitmaps are stored in a bitmap array, and finally the function returns the array back to the calling function.
Upvotes: 1
Reputation: 1934
You're looking for method in Bitmap that returns subset of source you can simply take the whole bitmaps width and height, divide by the amount of pieces and loop through every piece.
Upvotes: 0
Reputation: 3288
Use this method to divide the image.
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)
Upvotes: 0
Reputation: 7452
You can crop part of an image using the method below where image is the source image and r the rectangle section you'd like to crop. You can do it 4 times with the following rectangles:
Notice the method below is implemented in C#, using Mono for Android, but it should be almost identical in Java.
private Android.Graphics.Bitmap Crop(Android.Graphics.Bitmap image, Rectangle r)
{
return Android.Graphics.Bitmap.CreateBitmap(image, r.X, r.Y, r.Width, r.Height);
}
Upvotes: 0