Android 如何在 ImageView 上应用蒙版? [英] Android how to apply mask on ImageView?

查看:67
本文介绍了Android 如何在 ImageView 上应用蒙版?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试了这里的代码:创建带有掩码的 ImageView.我使用以下图像作为原始图像和蒙版:

So I tried the code from here: Creating an ImageView with a mask. I'm using the following images as original and mask:

然而,我得到的结果是这样的:

However, the result I get is this:

请注意,窗口背景不是黑色,而是全息光(在银河系中看起来像非常浅的灰色,而不是全白).第二张图片是我在列表视图中选择一个项目时得到的结果.

Note that the window background is not black, but holo light (which on the galaxy nexus looks like a very pale gray, not completely white). The second image is the result I get when an item is selected on a list view.

如果我使用相同的算法创建一个新的位图,然后将它传递给图像视图而不是覆盖 onDraw(),它会正确绘制:

If instead I create a new Bitmap using the same algorithm and then pass it to the image view instead of overriding onDraw(), it draws correctly:

Canvas canvas = new Canvas();
Bitmap mainImage = //get original image
Bitmap maskImage = //get mask image
Bitmap result = Bitmap.createBitmap(mainImage.getWidth(), mainImage.getHeight(), Bitmap.Config.ARGB_8888);

canvas.setBitmap(result);
Paint paint = new Paint();
paint.setFilterBitmap(false);

canvas.drawBitmap(mainImage, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(maskImage, 0, 0, paint);
paint.setXfermode(null);

imageView.setImageBitmap(result);

我得到了预期的结果:

注意淡入淡出是正确应用的.这在做出选择时更加明显.

Note the fade is correctly applied. This is more evident when a selection is made.

那么 ImageView 的 onDraw 方法是怎么回事来创建这个黑色背景而不是让窗口背景显示出来?有趣的是,如果原始图像本身具有一定的透明度,则该透明度会受到尊重,例如:

So what's going on on ImageView's onDraw method to create this black backdrop instead of letting the window background show through? What's interesting is that if the original image itself has some transparency, that transparency is respected, for example:

我自己无法弄清楚.我宁愿能够在 onDraw 上执行它而不是预先创建位图,因为它仅适用于作为源和掩码的位图.我希望能够使用渐变和纯色等其他可绘制对象来实现,但在这些情况下,宽度和高度未设置.

I can't figure it out by myself. I'd rather be able to do it on onDraw instead of pre-creating the bitmap because it only works for bitmaps as source and mask. I want to be able to do it with other drawables like gradients and solid colours but on those cases the width and height are not set.

推荐答案

在研究了所有 stackoverflow 帖子后,我找到了创建无黑边遮罩的完美组合.它非常适合我的目的.

I have found the perfect combination for creating masking without black border after researching through all the stackoverflow posts. It suits my purpose quite well.

目前我正在使用一个普通图像和一个遮罩图像(具有透明度的 png)创建一个可拖动视图,因此我需要覆盖 onDraw 函数.

Currently I'm creating a draggable view using one normal image and a masking image (a png with transparency), so I'll need to override the onDraw function.

private Bitmap mImage = ...;
private Bitmap mMask = ...;  // png mask with transparency
private int mPosX = 0;
private int mPosY = 0;

private final Paint maskPaint;
private final Paint imagePaint;

public CustomView (final Context context) {
    maskPaint = new Paint();
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    imagePaint = new Paint();
    imagePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
}

/* TODO
 if you have more constructors, make sure you initialize maskPaint and imagePaint
 Declaring these as final means that all your constructors have to initialize them.
 Failure to do so = your code won't compile.
*/

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();      
    canvas.drawBitmap(mMask, 0, 0, maskPaint);
    canvas.drawBitmap(mImage, mPosX, mPosY, imagePaint);
    canvas.restore();
}

这篇关于Android 如何在 ImageView 上应用蒙版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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