Android自定义相机镂空遮罩

public class MaskView extends View {

    /**
     * 遮罩颜色
     */
    private int maskColor = Color.argb(100, 0, 0, 0);

    /**
     * 镂空矩形
     */
    private Rect frame = new Rect();

    /**
     * 镂空边框
     */
    private Paint border = new Paint(Paint.ANTI_ALIAS_FLAG);

    /**
     * 镂空区域
     */
    private Paint eraser = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Path path = new Path();

    public MaskView(Context context) {
        super(context);
        init();
    }

    public MaskView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MaskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 硬件加速不支持,图层混合。
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        // 取景框颜色、线宽
        border.setColor(Color.WHITE);
        border.setStyle(Paint.Style.STROKE);
        border.setStrokeWidth(5);

        eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

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

        int width = (int) (w * 0.7f);
        int height = (int) (h * 0.7f);

        int left = (w - width) / 2;
        int top = (h - height) / 2;
        int right = width + left;
        int bottom = height + top-150;

        frame.left = left;
        frame.top = top;
        frame.right = right;
        frame.bottom = bottom;

    }


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

        int left = frame.left;
        int top = frame.top;
        int right = frame.right;
        int bottom = frame.bottom;

        fillRectRound(left, top, right, bottom, 200, 200);

        canvas.drawColor(maskColor);
        canvas.drawPath(path, border);
        canvas.drawPath(path, eraser);

    }

    private void fillRectRound(float left, float top, float right, float bottom, float rx, float ry) {
        path.reset();

        float width = right - left;
        float height = bottom - top;

        float lineWidth = (width - (2 * rx));
        float lineHeight = (height - (2 * ry));

        path.moveTo(left, top + ry);
        path.rQuadTo(0, -ry, rx, -ry);
        path.rLineTo(lineWidth, 0);
        path.rQuadTo(rx, 0, rx, ry);
        path.rLineTo(0, lineHeight);
        path.rQuadTo(0, ry, -rx, ry);
        path.rLineTo(-lineWidth, 0);
        path.rQuadTo(-rx, 0, -rx, -ry);
        path.rLineTo(0, -lineHeight);

        path.close();

//        RectF roundRect = new RectF(left, top, right, bottom);
//        path.addRoundRect(roundRect, rx, ry, Path.Direction.CW);

    }

    public Rect getFrameRect() {
        return new Rect(frame);
    }

}

//引用

MaskView maskView =new MaskView(this); addContentView(maskView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值