【问题标题】:How to use masks in android如何在android中使用掩码
【发布时间】:2012-01-11 21:59:16
【问题描述】:

我正在尝试使用面具。 我想使用一个图像来暴露底层图像的一部分。 例如。我有一个箭头,它暴露了底层(红色)正方形的一部分。 我的问题是,尽管遮罩有效,但未暴露的任何内容都呈现为黑色矩形,而我想要透明背景。我的箭头图像有一个透明的画布。

我的代码是:

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/ 来了解我的意思

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要在MaskAttempt中再添加一个Bitmap

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 2018-09-17
      • 1970-01-01
      • 2018-10-19
      • 2013-10-10
      相关资源
      最近更新 更多