package com.sheep.gamegroup.view.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * Created by ljy on 2018/3/12.
 */

public class TextProgressBar extends ProgressBar {
    private String str;
    private Paint mPaint;
    public TextProgressBar(Context context) {
        super(context);
        initText();
    }

    public TextProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initText();
    }

    public TextProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initText();
    }

    public TextProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initText();
    }

    @Override
    public synchronized void setProgress(int progress) {
        setText(progress);
        super.setProgress(progress);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        this.mPaint.getTextBounds(this.str, 0, this.str.length(), rect );
        int x = getWidth() / 2 - rect.centerX();// 让字体处于中间位置
        int y = getHeight() / 2 - rect.centerY();//让显示的字体处于中间位置
        canvas.drawText(this.str, x, y, this.mPaint);
    }

    /**
     *初始化，画笔
     */
    private void initText(){
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);//设置抗锯齿
        this.mPaint.setColor(Color.WHITE);
    }
    private void setText(int propress){
        int i = (int) ((propress *1.0f / this.getMax()) * 100);
        this.str = String.valueOf(i) + "%";
    }
}
