package com.xunao.effectdemo.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/911:26
 * desc :
 * version: 1.0
 */
public class MaskFilterView extends View {
	private Paint lightPaint;
	private int centerX, centerY;
	/** 发光范围 */
	private int radioRadius = 20;

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

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

	private void init() {
		lightPaint = new Paint();
		setLayerType(LAYER_TYPE_SOFTWARE, null);
		lightPaint.setColor(Color.parseColor("#EC3E3E"));
		lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.INNER));
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		centerX = getLeft() + getMeasuredWidth()/2;
		centerY = getTop() + getMeasuredHeight()/2;
		super.onMeasure(widthMeasureSpec, widthMeasureSpec);
	}

	public void setBlurType(int blurType) {
		switch (blurType) {
			case 0:
				lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.INNER));
				break;
			case 1:
				lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.NORMAL));
				break;
			case 2:
				lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.SOLID));
				break;
			case 3:
				lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.OUTER));
				break;
		}

		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();

//去锯齿

		paint.setAntiAlias(true);

		paint.setStyle(Paint.Style.FILL);

		paint.setColor(Color.RED);

//		paint.setStrokeWidth(3);

		Path path3 = new Path();

		path3.moveTo(60, 340);

		path3.lineTo(180, 340);

		path3.lineTo(120, 250);

		path3.close();


		canvas.drawPath(path3, lightPaint);

//		canvas.drawCircle(centerX, centerY, 150, lightPaint);
	}
}

