Android Bitmap Masking (Xfermode) 留下不透明的黑色背景 [英] Android Bitmap Masking (Xfermode) leaves opaque black background behind

查看:89
本文介绍了Android Bitmap Masking (Xfermode) 留下不透明的黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图,并且在 onDraw() 中,我正在尝试执行位图屏蔽.我有一个 squareBitmap(红色)填充整个视图,我有一个 circleBitmap(蓝色)作为遮罩.我正在使用模式:PorterDuff.Mode.DST_IN.

I have a custom view and in the onDraw(), I am trying to perform bitmap masking. I have a squareBitmap (red color) which fills the whole view, and I have a circleBitmap (blue color) that acts as the mask. I am using the mode: PorterDuff.Mode.DST_IN.

我期待的结果是一个红色实心圆圈.我明白了,但我也得到了黑色不透明背景.我不想要这种不透明的背景,而是应该是透明的.Figure 1 是我得到的结果,Figure 2 是我要找的结果.

The result I am expecting is a RED filled circle. I get that, but I also get a BLACK opaque background. I don't want this opaque background, rather it should be transparent. Figure 1 is the result I got, and Figure 2 is the result that I am looking for.

我的代码:(为了这个问题,我已经把所有东西都移到了 onDraw() 中)

My code: (I have moved everything inside onDraw() for the purpose of this question)

protected void onDraw(Canvas canvas) {

    final int width = canvas.getWidth();
    final int height = canvas.getHeight();

    Bitmap circleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas circleCanvas = new Canvas(circleBitmap);
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setStyle(Paint.Style.FILL_AND_STROKE);
    p.setColor(Color.BLUE);
    circleCanvas.drawCircle(width / 2, height / 2, width / 2, p);

    p.setColor(Color.RED);
    Bitmap squareBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas squareCanvas = new Canvas(squareBitmap);
    final Rect squareRect = new Rect(0, 0, width, height);
    squareCanvas.drawRect(squareRect, p);

    Paint q = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvas.drawBitmap(squareBitmap, 0, 0, q);
    q.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(circleBitmap, 0, 0, q);
    q.setXfermode(null);
}


我哪里错了?如何避免这种黑色不透明背景?


Where am I going wrong? How can I avoid this black opaque background?

推荐答案

似乎要完成 Xfermode,我需要将绘图重定向到屏幕外位图.所以添加以下内容解决了我的问题.

It seems that to get the Xfermode done, I need to redirect the drawing to an offscreen bitmap. So adding the following fixed my problem.

canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), q);

但话说回来,saveLayer文档说避免使用这种方法,因为它很昂贵.建议使用硬件层代替这种方法.

But then again, the documentation of saveLayer says to avoid using this method since it is expensive. It is suggested to use a harware layer instead of this method.

避免使用这种方法,特别是如果提供的边界很大,或者如果从 saveFlags 中省略了 CLIP_TO_LAYER_SAVE_FLAG范围.建议在 View 上使用硬件层应用 xfermode、滤色器或 alpha,因为它会执行很多比这个方法好.

Avoid using this method, especially if the bounds provided are large, or if the CLIP_TO_LAYER_SAVE_FLAG is omitted from the saveFlags parameter. It is recommended to use a hardware layer on a View to apply an xfermode, color filter, or alpha, as it will perform much better than this method.

因此,在 saveLayer 方法中,我能够通过添加以下内容来修复它:

Hence, intead of saveLayer method, I was able to fix it by adding the following:

setLayerType(LAYER_TYPE_HARDWARE, q);

这篇关于Android Bitmap Masking (Xfermode) 留下不透明的黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆