Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a bitmap on a canvas, respecting alpha values of the bitmap?

background

i have a master bitmap that i need to draw on it other bitmaps.

the master bitmap has some semi-transparent pixels (pixels with variant values for the alpha channel) , so that the other bitmaps that are drawn on it should be merged with it instead of overriding the colors completely.

the question

how can i set the canvas to draw the bitmaps on the master bitmap with respect to the semi-transparent pixels ?

note: the alpha is not for the whole bitmap/s . it's per pixel.

like image 819
android developer Avatar asked Dec 20 '22 03:12

android developer


2 Answers

Canvas.setXfermode(Xfermode xfermode). There are a number of Xfermodes you can choose.

like image 84
techiServices Avatar answered Jan 31 '23 00:01

techiServices


public void putOver(Bitmap master, Bitmap alphaBitmap){
    Canvas canvas = new Canvas(matter);
    Paint paint = new Paint();
    paint.setXferMode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
    canvas.drawBitmap(left, top, left+alphaBitmap.width, left+alphaBitmap.height, paint);
}
like image 33
Desmond Yao Avatar answered Jan 31 '23 02:01

Desmond Yao