本文介绍了屏蔽Android上绘制对象/位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在寻找一种方式来使用黑白位图掩盖在Android另一个位图或绘制对象的alpha通道。我很好奇,怎么做到这一点的最好办法是。当然,我有几个想法如何做到这一点,但他们不是最优的。
I'm currently looking for a way to use a black and white bitmap to mask the alpha channel of another bitmap or Drawable on Android. I'm curious as to what the best way to do this is. I certainly have a couple of ideas for how to do this, but they are not optimal.
我需要能够申请一个新的面具形象,每隔一段时间(黑白位图将改变每隔几秒钟)。
I need to be able to apply a new mask to the image every so often (the black and white bitmap will change every few seconds).
在如何实现这一目标将大大AP preciated任何反馈。
Any feedback on how to achieve this would be greatly appreciated.
推荐答案
我得到了它的工作,所以它的东西,像这样
I got it working, so it's something like this
// 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();
这篇关于屏蔽Android上绘制对象/位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!