How to draw a bitmap on a canvas, respecting alpha

2019-02-18 15:43发布

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.

3条回答
对你真心纯属浪费
2楼-- · 2019-02-18 15:53
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);
}
查看更多
女痞
3楼-- · 2019-02-18 16:18

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

查看更多
Root(大扎)
4楼-- · 2019-02-18 16:18
    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.

查看更多
登录 后发表回答