/** Creates and returns a new bitmap which is the same as the provided bitmap
* but with horizontal or vertical padding (if necessary)
* either side of the original bitmap
* so that the resulting bitmap is a square.
* @param bitmap is the bitmap to pad.
* @return the padded bitmap.*/
public static Bitmap padBitmap(Bitmap bitmap)
{
int paddingX;
int paddingY;
if (bitmap.getWidth() == bitmap.getHeight())
{
paddingX = 0;
paddingY = 0;
}
else if (bitmap.getWidth() > bitmap.getHeight())
{
paddingX = 0;
paddingY = bitmap.getWidth() - bitmap.getHeight();
}
else
{
paddingX = bitmap.getHeight() - bitmap.getWidth();
paddingY = 0;
}
Bitmap paddedBitmap = Bitmap.createBitmap(
bitmap.getWidth() + paddingX,
bitmap.getHeight() + paddingY,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(paddedBitmap);
canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF); // this represents white color
canvas.drawBitmap(
bitmap,
paddingX / 2,
paddingY / 2,
new Paint(Paint.FILTER_BITMAP_FLAG));
return paddedBitmap;
}
Monday, 22 October 2012
Android - how to add padding or a border to a Bitmap
Here's a neat (I'd like to think) method (which you can tinker with) for adding padding or a border to a Bitmap:
Subscribe to:
Post Comments (Atom)
1 comment:
I found this useful, thanks.
Post a Comment