如何使用Android的面具 [英] How to use masks in android

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

问题描述

我想使用口罩。 我想用一个图像,露出底层图像的一部分。 例如。我有一个公开的底层(红色)广场的一部分,一个箭头。 我的问题是,虽然面具作品,什么是不暴露呈现为黑色矩形,而我希望有一个透明的背景。我的箭头图像具有透明画布。

I am trying to use masks. I want to use one image to expose part of an underlying image. E.g. I have an arrow which exposes part of an underlying (red) square. My problem is that although the mask works, anything which is not exposed is rendered as a black rectangle, whereas I want a transparent background. My arrow image has a transparent canvas.

我的code是:

private class MaskAttempt extends View {

        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        private Bitmap mItemToBeMasked;
        private Bitmap mMask;

        public MaskAttempt(Context context) {
            super(context);
            this.setBackgroundColor(Color.WHITE);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

                final Resources res = context.getResources();
            mItemToBeMasked = BitmapFactory.decodeResource(res, R.drawable.red_rectangle);
            mMask = BitmapFactory.decodeResource(res, R.drawable.icon_mask);
        }

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

            canvas.translate((getWidth() - mItemToBeMasked.getWidth()) >> 1, (getHeight() -     mItemToBeMasked.getHeight()) >> 1);

            canvas.drawBitmap(mItemToBeMasked, 0, 0, null);
            canvas.drawBitmap(mMask, 0, 0, mPaint);

            canvas.restore();
        }

您可以看到我的意思是看 HTTP://www.steveharris100.pwp。 blueyonder.co.uk/

You can see what I mean by looking at http://www.steveharris100.pwp.blueyonder.co.uk/

推荐答案

您需要添加更多的位图 MaskAttempt

public MaskAttempt(Context context) {
    super(context);
    this.setBackgroundColor(Color.WHITE);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    final Resources res = context.getResources();
    mItemToBeMasked = BitmapFactory.decodeResource(res, R.drawable.red_rectangle);
    mMask = BitmapFactory.decodeResource(res, R.drawable.icon_mask);
    duplicate = BitmapFactory.decodeResource(res, R.drawable.icon_mask).copy(Config.ARGB_8888, true);

    c = new Canvas(duplicate);

    x = new Paint(Paint.ANTI_ALIAS_FLAG);
    x.setColor(Color.BLACK);
    x.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}

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

    canvas.translate((getWidth() - mItemToBeMasked.getWidth()) >> 1, (getHeight() -     mItemToBeMasked.getHeight()) >> 1);

    c.drawBitmap(mItemToBeMasked, 0, 0, null);
    c.drawBitmap(mMask, 0, 0, mPaint);
    canvas.drawBitmap(duplicate, 0, 0, null);

    canvas.restore();
}

这篇关于如何使用Android的面具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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