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

4

5

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.

Comprehend answered 9/7, 2013 at 15:27 Comment(3)
you have two differents bitmap?Brachycephalic
yes, and even more. now that i think about it, maybe setting something with the paint object would do the job.Comprehend
See my answer at: https://mcmap.net/q/672698/-how-to-make-canvas-drawbitmap-transparentSynonymy
U
8

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

Uppity answered 9/7, 2013 at 16:28 Comment(4)
the correct one seems to be DST_ATOP : paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));Comprehend
@android developer. i would've had to say you need to test which is right for you ;)Uppity
yes what i wanted is that if there is alpha, it will be merged with the other bitmap depending on the value of the alpha, just like when you use a painting app and you crop an area to an area that has semi transparency.Comprehend
+100 for solution, searching for 3 hours on the internet but this is only solution that works :)Kravits
P
4
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);
}
Philipp answered 4/4, 2015 at 4:39 Comment(1)
Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.Eryneryngo
E
0
    public Bitmap PutoverChange(Bitmap all, Bitmap scaledBorder) {
    Paint paint = new Paint();
    final int width = change.getWidth();
    final int height = change.getHeight();
    patt = Bitmap.createScaledBitmap(change, width, height, true);
    Bitmap mutableBitmap = patt.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    scaledBorder = Bitmap.createScaledBitmap(border, width, height, true);
    paint.setAlpha(100);
    canvas.drawBitmap(scaledBorder, 0, 0, paint);
    return mutableBitmap;

}

here the transparency is 100. you can modify it to 50 so it becomes semi transparent.

Excise answered 9/7, 2013 at 17:18 Comment(1)
i've already written that i do not want to set the transparency of the entire bitmap. there are some pixels that have transparency, not all, and some have a different value of transparency.Comprehend
A
0

For use any Bitmap as alpha mask:

*1. Draw mask bitmap on Canvas.

*2. Use PorterDuff.Mode.SRC_IN for Paint.

*3. Draw source bitmap.

static Bitmap getBitmap( Bitmap bm_source, Bitmap bm_mask ){
    Bitmap bm = Bitmap.createBitmap( bm_source.getWidth(), bm_source.getHeight(), Bitmap.Config.ARGB_8888 );
    Canvas canvas = new Canvas( bm );
    canvas.drawBitmap( bm_mask, 0, 0, null );
    Paint paint = new Paint( Paint.ANTI_ALIAS_FLAG );
    paint.setXfermode( new PorterDuffXfermode( PorterDuff.Mode.SRC_IN ));
    canvas.drawBitmap( bm_source, 0, 0, paint );
    return bm;
}

enter image description here

Abrahamsen answered 1/3, 2024 at 9:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.