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.

helios
  • 13,574
  • 2
  • 45
  • 55
android developer
  • 114,585
  • 152
  • 739
  • 1,270

3 Answers3

7

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

techi.services
  • 8,473
  • 4
  • 39
  • 42
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);
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Desmond Yao
  • 545
  • 1
  • 4
  • 10
  • 2
    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. – Nathan Tuggy Apr 04 '15 at 05:01
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.

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • 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. – android developer Jul 09 '13 at 18:18