PorterDuff masking leaves opaque black background

↘锁芯ラ 提交于 2019-12-04 19:26:52

try this:

public class MaskedLayout extends FrameLayout {

    private NinePatchDrawable mMask;

    public MaskedLayout(Context context) {
        this(context, null);
    }

    public MaskedLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MaskedLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMask = (NinePatchDrawable) getResources().getDrawable(R.drawable.mask);
        mMask.getPaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mMask.setBounds(0, 0, w, h);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        mMask.draw(canvas);
        canvas.restore();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!