更新时间:2023-01-23 13:37:30
您需要添加更多的位图
在 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();
}