PhotoBgView_50.java 7.27 KB
package com.sw.laryngoscope.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;

import com.sw.laryngoscope.R;
import com.sw.laryngoscope.common.Constant;
import com.sw.laryngoscope.manager.SpManager;

/**
 * 电量显示控件
 */
public class PhotoBgView_50 extends androidx.appcompat.widget.AppCompatImageView {

    private Paint mPaint;
    private float angle;// 旋转角度
    private float mWidth;
    private float mHeight;
    public static final int LEFT = 270;
    public static final int RIGHT = 90;public static final int UP = 0;
    public static final int DOWN = 180;

    public PhotoBgView_50(Context context) {
        super(context);
        init(context, null, 0);
    }

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

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

    private void init(Context context, AttributeSet attrs, int defStyle) {
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        //mPaint.setColor(Color.BLACK);
        mPaint.setColor(Color.parseColor("#1B2027"));
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(1);
    }

    public PhotoBgView_50 setWidth(float width){
        this.mWidth = width;
        return this;
    }

    public PhotoBgView_50 setHeight(float height){
        this.mHeight = height;
        return this;
    }

    /**
     *
     * @Title: setAngle
     * @Description: 按角度调整箭头方向 0-360
     * @param @param angle
     * @return TriangleView this
     * @throws
     */
    public PhotoBgView_50 setAngle(float angle) {
        this.angle = angle;
        invalidate();
        return this;
    }

    /**
     *
     * @Title: setDirection
     * @Description: 调整箭头方向  LEFT,RIGHT,UP,DOWN
     * @param @param direction
     * @return TriangleView this
     * @throws
     */
    public PhotoBgView_50 setDirection(int direction){
        this.angle = direction;
        invalidate();
        return this;
    }

    /**
     *
     * @Title: setColor
     * @Description: 给三角形填充颜色
     * @param @param color
     * @return TriangleView this
     * @throws
     */
    public PhotoBgView_50 setColor(int color){
        mPaint.setColor(color);
        invalidate();
        return this;
    }

    private void drawTriangle(Canvas canvas) {
        mWidth = getWidth();
        mHeight = getHeight();
        if ( SpManager.get_border_type() == Constant.BORDER_TYPE_A ) {
            Path p = new Path();
            //p.addCircle(mWidth / 2f, mHeight / 2f, mWidth / 2f, Path.Direction.CW);
            p.addRoundRect(new RectF(0, 0, mWidth, mHeight), 50, 50, Path.Direction.CW);

            canvas.clipPath(p, Region.Op.DIFFERENCE);
            canvas.drawColor(0, PorterDuff.Mode.CLEAR);
            //canvas.restore();
            //canvas.drawColor(getResources().getColor(R.color.transparent));
            canvas.drawColor(Color.parseColor("#1B2027"));
        }

        if ( SpManager.get_border_type() == Constant.BORDER_TYPE_B ) {
            int sideLen = (int) (mWidth / 12f * 1.0f);
            Path path0 = new Path();
            path0.moveTo(0, 0);
            path0.lineTo(sideLen, 0);
            path0.lineTo(0, sideLen);

            Path path1 = new Path();
            path1.moveTo(mWidth - sideLen, 0);
            path1.lineTo(mWidth, 0);
            path1.lineTo(mWidth, sideLen);

            Path path2 = new Path();
            path2.moveTo(0, mHeight - sideLen);
            path2.lineTo(0, mHeight);
            path2.lineTo(sideLen, mHeight);

            Path path3 = new Path();
            path3.moveTo(mWidth - sideLen, mHeight);
            path3.lineTo(mWidth, mHeight);
            path3.lineTo(mWidth, mHeight - sideLen);
            canvas.save();
            canvas.drawPath(path0, mPaint);
            canvas.drawPath(path1, mPaint);
            canvas.drawPath(path2, mPaint);
            canvas.drawPath(path3, mPaint);
            canvas.restore();
        }

        if ( SpManager.get_border_type() == Constant.BORDER_TYPE_C ) {
            Path p = new Path();
            //p.addCircle(mWidth / 2f, mHeight / 2f, mWidth / 2f, Path.Direction.CW);
            p.addCircle(mWidth / 2f, mHeight / 2f, mHeight / 2f, Path.Direction.CW);

            canvas.clipPath(p, Region.Op.DIFFERENCE);
            canvas.drawColor(0, PorterDuff.Mode.CLEAR);
            //canvas.restore();
            //canvas.drawColor(getResources().getColor(R.color.transparent));
            canvas.drawColor(Color.parseColor("#1B2027"));
        }

        if ( SpManager.get_border_type() == Constant.BORDER_TYPE_D ) {
        }

        //Rect rect = new Rect(0, 0, (int) mWidth, (int) mHeight);

        /*Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.TRANSPARENT);//.setColor(Color.parseColor("#1B2027"));
        canvas.drawRect(rect, paint);
        canvas.save();*/

        //红色的方形
        /*Rect rect = new Rect(0, 0, (int) mWidth, (int) mHeight);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#1B2027"));
        //canvas.drawRect(rect, paint);

        //这里开始保存渲染层,从 canvas.saveLayer()方法  到  canvas.restoreToCount()方法结束,期间不会影响其他绘制
        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        //PorterDuff.Mode.CLEAR 清除 蓝色的方形 所覆盖的面积
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        canvas.drawRoundRect(new RectF(0, 0, mWidth, mHeight), 100, 100, paint);

        //移除 Xfermode
        paint.setXfermode(null);
        //恢复画布状态
        canvas.restoreToCount(layerId);*/

        /*Paint _debugInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //设置无锯齿 也可以使用setAntiAlias(true)
        _debugInfoPaint.setColor(Color.GREEN);//设置画笔颜色
        _debugInfoPaint.setAlpha(200);
        _debugInfoPaint.setStrokeWidth(1.5f);//设置线宽
        _debugInfoPaint.setStyle(Paint.Style.STROKE);//设置样式:FILL表示颜色填充整个;STROKE表示空心
        canvas.drawRoundRect(new RectF(0, 0, mWidth, mHeight), 20, 20, _debugInfoPaint);*/
    }

    /**
     *
     * @Title: calTriangleSileLength
     * @Description: 计算三角形边长
     * @param @param diameter
     * @param @return
     * @return float
     * @throws
     */
    private float calTriangleSileLength(float diameter) {
        return (float) (2 * Math.sqrt(Math.pow(diameter / 2, 2)
                - Math.pow(diameter / 4, 2)));
    }

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

}