首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Android上屏蔽可绘制/位图

在Android上屏蔽可绘制/位图
EN

Stack Overflow用户
提问于 2010-04-13 01:08:17
回答 5查看 21.3K关注 0票数 18

我目前正在寻找一种方法来使用黑白位图来遮罩另一个位图或可在Android上绘制的alpha通道。我很好奇做这件事最好的方法是什么。我当然有几个关于如何做到这一点的想法,但它们并不是最佳的。

我需要能够将新的蒙版应用到图像中(黑白位图每隔几秒钟就会改变一次)。

任何关于如何实现这一点的反馈都将非常感谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-10-13 04:19:48

我让它工作了,所以它是这样的

代码语言:javascript
复制
    // we first same the layer, rectF is the area of interest we plan on drawing
    // this will create an offscreen bitmap
    canvas.saveLayer(rectF, null, Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
            | Canvas.CLIP_TO_LAYER_SAVE_FLAG);

    // draw our unmasked stuff
    super.draw(canvas);
    // We same a layer again but this time we pass a paint object to mask
    // the above layer
    maskPaint = new Paint()
    maskPaint.setColor(0xFFFFFFFF);
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));

    canvas.saveLayer(rectF, maskPaint,
            Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
                    | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                    | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                    | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    // we draw the mask which is black and white. In my case
    // I have a path, and I use a blurPaint which blurs the mask slightly
    // You can do anti aliasing or whatever you which. Just black & white
    canvas.drawPath(path, blurPaint);
    // We restore twice, this merges the results upward
    // as each saveLayer() allocates a new drawing bitmap
    canvas.restore();
    canvas.restore();
票数 6
EN

Stack Overflow用户

发布于 2014-07-31 00:04:06

我的解决方案与@over_optimistic的解决方案非常接近,只需少一次saveLayer()调用。我使用可抽出的遮罩而不是路径,在我的例子中它是一张光盘。

我将这些变量声明为字段(最好在onDraw方法之外分配内存):

代码语言:javascript
复制
private Paint maskingPaint = new Paint();
private Drawable mask = <insert your drawable here>

然后,在onDraw()之外的某个地方设置对象:

代码语言:javascript
复制
// Xfermode won't work if hardware accelerated
setLayerType(View.LAYER_TYPE_SOFTWARE, null);

// Using destination shape as a mask
// For a good explanation of PorterDuff transfer modes : http://ssp.impulsetrain.com/porterduff.html
maskingPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
maskingPaint.setAntiAlias(true);

// Position the mask
mask.setBounds(<insert your mask bounds here>);

最后,onDraw()方法应用掩码:

代码语言:javascript
复制
@Override
protected synchronized void onDraw(Canvas canvas)
{
    // Draw the mask first, making it the PorterDuff destination
    mask.draw(canvas);

    // Save the layer with the masking paint, that will be applied on restore()
    // Using CLIP_TO_LAYER_SAVE_FLAG to avoid any overflow of the masked image outside the mask bounds.
    Rect bounds = mask.getBounds();
    canvas.saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, maskingPaint, 
            Canvas.CLIP_TO_LAYER_SAVE_FLAG);

    // Draw the shape offscreen, making it the PorterDuff source
    super.onDraw(canvas);

    // Apply the source to the destination, using SRC_IN transfer mode
    canvas.restore();
}

为了更好地理解传输模式,我参考了http://ssp.impulsetrain.com/porterduff.html。这个页面读起来很有趣。在那之后,用同样的代码,你将能够获得比简单的掩码更多的东西!

票数 8
EN

Stack Overflow用户

发布于 2014-07-26 19:40:17

我做了一个可屏蔽的布局。这是一个框架布局,您可以在其中指定xporterduffmode和掩码。你可以在这里找到它:https://github.com/christophesmet/android_maskable_layout

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2623888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档