package com.zhaohaoting.framework.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.utils.DensityUtil;

/**
 * author：Created by ZHT on 2018/2/26 17:00
 * email：526309416@qq.com
 * desc：圆形文字绘制
 */
public class DrawTextView extends View {
    //像素 需要时可做成自定义属性
    private static final int BORDER_WIDTH = 2;//dp
    private int mBorderWidth = 0;//dp
    private static final int TEXT_SIZE_PX = 16;//SP
    private int textSize;
    private Paint bgPaint;
    private Canvas bgCanvas;
    private Paint borderPaint;
    private Paint textPaint;
    private Canvas textCanvas;
    private Paint.FontMetrics fontMetrics;
    private int diameter;
    private Bitmap mBgBitmap;
    private Bitmap mTextBitmap;
    private PorterDuffXfermode xfermode;
    private Paint mPaint;
    private CharSequence text;

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

    public DrawTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 对象初始化慎放在onDraw方法中 onDraw有可能会执行数次
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public DrawTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        textSize = DensityUtil.dip2px(context, TEXT_SIZE_PX);
        bgPaint = new Paint();
        borderPaint = new Paint();
        textPaint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速
        mBorderWidth = DensityUtil.dip2px(context, BORDER_WIDTH);
        //绘制背景
        bgPaint.setColor(getResources().getColor(R.color.colorAccent));
        bgPaint.setAntiAlias(true);
        //绘制文字
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);
        textPaint.setColor(getResources().getColor(R.color.white));
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setFakeBoldText(true);
        fontMetrics = textPaint.getFontMetrics();
        //绘制边框
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(getResources().getColor(R.color.colorAccent));
        borderPaint.setStrokeWidth(mBorderWidth);
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
        //图像混合使用
        mPaint = new Paint();
    }

    public final void setText(CharSequence text) {
        this.text = text;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (TextUtils.isEmpty(text)) {
            return;
        }
        diameter = Math.min(getWidth(), getHeight());//在测量完成后才能获取宽高
        initBitmap();
        canvas.drawBitmap(mBgBitmap, 0, 0, mPaint);
        mPaint.setXfermode(xfermode);
        canvas.drawBitmap(mTextBitmap, 0, 0, mPaint);
        mPaint.setXfermode(null);
        //绘制圆形边框
        canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2 - mBorderWidth / 2, borderPaint);
        //资源回收
        mBgBitmap.recycle();
        mTextBitmap.recycle();
    }

    private void initBitmap() {
        //创建蓝色背景
        mBgBitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
        bgCanvas = new Canvas(mBgBitmap);
        bgCanvas.drawCircle(diameter / 2, diameter / 2, diameter / 2, bgPaint);
        //绘制文字
        mTextBitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
        textCanvas = new Canvas(mTextBitmap);
        int baseLineY = (int) (getHeight() / 2 - fontMetrics.bottom / 2 - fontMetrics.top / 2);//基线中间点的y轴计算公式
        textCanvas.drawText(text.toString(), getHeight() / 2, baseLineY, textPaint);
    }

    public CharSequence getText() {
        return text;
    }
}
