package com.example.iot_controlhost.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.SurfaceView;

/**
 * 带圆形遮罩的相机预览框
 */
public class CircleOverlay extends SurfaceView {
    /**
     * 半价范围
     * （1-3为合适范围）
     * 数字越大显示的范围越小
     */
    public static final int FACE_RADIUS = 3;
    private Paint mPaint;

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

    public CircleOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleOverlay(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setWillNotDraw(false); // 确保可以绘制内容
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAlpha(0);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

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

        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width, height) / CircleOverlay.FACE_RADIUS;

        // 绘制白色遮罩层
        canvas.drawARGB(250, 255, 255, 255);
        // 绘制透明圆形
        canvas.drawCircle(width / 2f, height / 2f, radius, mPaint);
    }
}
