机器人如何敷面膜的ImageView的? [英] Android how to apply mask on ImageView?

查看:189
本文介绍了机器人如何敷面膜的ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想在code从这里:创建的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:

请注意,窗口背景不是黑色的,但全息光(它在Galaxy Nexus的看起来像一个非常浅灰色,而不是全白)。第二个图像是结果,当一个项目被选中的列表视图我搞定了。

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);

我得到预期的结果:

I get the expected 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:

我不出来自己干。我宁愿能做到pre-创建位图,因为它仅适用于位图作为源和掩模的它的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.

推荐答案

我已经找到了完美结合,为创建无黑边通过所有的计算器职位研究后掩蔽。它适合我的目的相当不错的。

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();
}

这篇关于机器人如何敷面膜的ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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