圆形进度条

/**
 * Created by Cwm on 2018/3/6.
 * 圆形进度条
 */

public class Progress extends View {

    private int mExcircleColor = Color.RED;
    private int mFilletColor = Color.BLUE;
    private int mBreadth = 20;//20px
    private int mFontColor = Color.RED;
    private int mFontSize = 40;//20px
    //分别是外圆弧、内圆弧、字体画笔
    private Paint mExcirclePaint, mFilletPaint, mTextPaint;
    private String text = "0%";
    private float mProgress=0.0f;

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

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

    public Progress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Progress);
        mExcircleColor = typedArray.getColor(R.styleable.Progress_Progress_excircleColor, mExcircleColor);
        mFilletColor = typedArray.getColor(R.styleable.Progress_Progress_filletColor, mFilletColor);
        mFontColor = typedArray.getColor(R.styleable.Progress_Progress_fontColor, mFontColor);
        mBreadth = typedArray.getDimensionPixelSize(R.styleable.Progress_Progress_breadth, mBreadth);
        mFontSize = typedArray.getDimensionPixelSize(R.styleable.Progress_Progress_fontSize, mFontSize);
        typedArray.recycle();
        mExcirclePaint = initPaint(mExcircleColor, mBreadth, false);
        mFilletPaint = initPaint(mFilletColor, mBreadth, false);
        mTextPaint = initPaint(mFontColor, mFontSize, true);
    }

    //初始化画笔
    private Paint initPaint(int color, int paintSize, boolean flag) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        if (flag) {
            paint.setTextSize(paintSize);
        } else {
            paint.setStrokeWidth(paintSize);
        }
        paint.setStyle(Paint.Style.STROKE);
        return paint;
    }


    //重新测量设置View宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //获取模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //在测量宽高时判断模式是否为AT_MOST
        if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) {
            throw new UnsupportedOperationException("Width and height must specify specific values");
        }
        //在测量宽高时判断是否大于0
        if (widthSize <= 0 || heightSize <= 0) {
            throw new UnsupportedOperationException("Width and height must be greater than 0");
        }
        //当宽和高有具体值且大于0,但是宽和高的值不一样时,按照高度走,必须保持一个正方形
        //即:
        setMeasuredDimension(widthSize > heightSize ? heightSize : widthSize
                , widthSize > heightSize ? heightSize : widthSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF(mBreadth, mBreadth, getWidth() - mBreadth, getHeight() - mBreadth);
        canvas.drawArc(rectF, 0, 360, false, mExcirclePaint);
        canvas.drawArc(rectF, 0, 360*(mProgress/100), false, mFilletPaint);
        Rect rect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), rect);
        int dx = getWidth() / 2 - rect.width() / 2;
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(text, dx, baseLine, mTextPaint);
    }

    public void setProgress(float progress) {
        this.mProgress = progress;
        invalidate();
    }

    public void setText(String text) {
        this.text = text;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值