Pranav Sharma
Pranav Sharma

Reputation: 684

Android: Drawing a Transparent Bitmap on Canvas with some color

Its Becoming a headache for me , i have a transparent png image,,, i have decoded that into a bitmap and later added on to a canvas,

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), picList[0] , options);
Paint p = new Paint();      
canvas.drawBitmap(mBitmap, 0, 0, p);

later on when i am saving the canvas it is showing image but showing the background as black, to implement white color i have used some codes like

Paint p = new Paint();
p.setAlpha(color.white);
p.setColor(color.white);
canvas.drawColor(color.white);
canvas.drawPaint(p);

but yet the color is not seted white,,, kindly help me out ,, i want the backgroung saved image to be white. if there is any other logic i am missing. thanx helping out.

Upvotes: 1

Views: 6898

Answers (5)

clo wan
clo wan

Reputation: 1

good!

if your image has bad white (not really (255 255 255)), you can take tolerancy into account

for(int x=0;x<img.getWidth();x++)
{
    for(int y=0;y<img.getHeight();y++)
    {
        int cou = img.getPixel(x, y), tolerancy = 40//max 255, your image would be completely tranpsparent;

        if(Math.abs(Color.red(cou)-255)<tolerancy && 
           Math.abs(Color.green(cou)-255)<tolerancy &&  
           Math.abs(Color.blue(cou)-255)<tolerancy)
            paletteFond.setPixel(x, y, Color.TRANSPARENT);
    }
}

Upvotes: 0

Pranav Sharma
Pranav Sharma

Reputation: 684

well i have found out the correct option. its canvas.drawARGB(255,30,30,39); give the various ARGB values and get color on the canvas enjoy :D

Upvotes: 1

Veer
Veer

Reputation: 2071

you can call following on your bitmap after drawing on Canvas:

        for(int x=0;x<bitmap.getWidth();x++){
            for(int y=0;y<bitmap.getHeight();y++){
                if(bitmap.getPixel(x, y)==Color.BLACK){
                    bitmap.setPixel(x, y, Color.WHITE);
                }
            }
        }

If you want WHITE bg for your saved image, use WHITE else you can use TRANSPARENT.

Upvotes: 2

Veer
Veer

Reputation: 2071

You can try using Color.TRANSPARENT instead of Color.white..

Upvotes: 0

P Srinivas Goud
P Srinivas Goud

Reputation: 896

Before creating Paint object call canvas.drawColor(color.white); if this not work go with canvas.drawColor(Color.WHITE, PorterDuff.Mode.DARKEN);

Upvotes: 0

Related Questions