Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: padding left a bitmap with white color

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!

like image 785
Jed84 Avatar asked Aug 05 '11 13:08

Jed84


2 Answers

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;
}
like image 92
Deepak Avatar answered Oct 17 '22 09:10

Deepak


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;
}
like image 9
Linh Avatar answered Oct 17 '22 10:10

Linh