Android: padding left a bitmap with white color

12,597

Solution 1

You can instead create a new Bitmap with the extra padding number of pixels. Set this as the canvas bitmap and Color the entire image with the required color and then copy your bitmap.

public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
    Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
    Canvas can = new Canvas(outputimage);
    can.drawARGB(FF,FF,FF,FF); //This represents White color
    can.drawBitmap(Src, padding_x, padding_y, null);
    return outputimage;
}

Solution 2

public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, paddingTop, null);
    return outputBitmap;
}

public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}


public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, paddingLeft, 0, null);
    return outputBitmap;
}
Share:
12,597
Jed84
Author by

Jed84

Updated on July 28, 2022

Comments

  • Jed84
    Jed84 almost 2 years

    How to set all white the 10 rows on the left side of a Bitmap? I'v got a Bitmap that has to be padded on the left side. I thought i can create a new image iterate on the old one getpixel for each position and setpixel on the new one (white or colored) than return the new bitmap...is this wrong? Any suggestion? thanks a lot!

  • Jed84
    Jed84 almost 13 years
    does this works on android? i'v got a Bitmap (android class) image..ho to switch to BufferedImage? thanks!
  • Jed84
    Jed84 almost 13 years
    Perfect! thanks..just one more question..for x-offset you mean the first pixel left after padding?
  • Pierre
    Pierre about 10 years
    Lets say i have a bitmap with dimentions (w256, h104), wil this be able to make this image (w256, h128) by adding white spaces on top and bottom evenly without stretching the image?
  • Rahul Sharma
    Rahul Sharma almost 8 years
    Nice but it only add pixcels to top and left side of bitmap. nothing found for all 4 sides(top,left,right,bottom).
  • Addy
    Addy about 4 years
    @RahulSharma I just did this to give padding from all side. Hope it will help someone. Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888); Canvas can = new Canvas(outputimage); can.drawBitmap(Src, padding_x, padding_y, null); Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawBitmap(outputimage, 0, 0, null); return output;
  • Xam
    Xam about 3 years
    Thanks so much man. Your functions are easy to use. +1.