圆形进度条

<declare-styleable name="MyProgressBar">
    <attr name="CURRENT_PROGRESS" format="integer"></attr>
    <attr name="TOTAL_PROGRESS" format="integer"></attr>
    <attr name="PROGRESSBAR_COLOR" format="color"></attr>
    <attr name="PROGRESSBAR_WIDTH" format="dimension"></attr>
    <attr name="PROGRESSTEXT_COLOR" format="color"></attr>
    <attr name="PROGRESSBAR_SIZE" format="dimension"></attr>
    <attr name="PROGRESSBAR_REDIUS" format="dimension"></attr>
    <attr name="BACKGROUND" format="color"></attr>

</declare-styleable>


package com.example.along.myproject;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ProgressBar;

/**
 * Created by long on 2017/4/1.
 */

public class MyProgressBar extends ProgressBar {
    private final static int CURRENT_PROGRESS=0;
    private final static int TOTAL_PROGRESS=100;
    private final static int PROGRESSBAR_COLOR=0x0058AC00;
    private final static int PROGRESSBAR_WIDTH=20;
    private final static int PROGRESSTEXT_COLOR=0x00FFB43A;
    private final static int PROGRESSBAR_SIZE=25;
    private final static int PROGRESSBAR_REDIUS=30;
    private final static int BACKGROUND=0x00FFFFFF;
    private int Current_Progress;
    private int Total_Progress;
    private int ProgressBar_Color;
    private int ProgressBar_Width;
    private int ProgressText_Color;
    private int ProgressText_SIZE;
    private int Progress_Redius;
    private Paint ProgressBarPaint;//进度条的画笔
    private Paint ProgressTextPaint;//字体的画笔
    private int BackGround;
    private int Width;
    private int Height;
    public MyProgressBar(Context context) {
        this(context,null);
    }

    public MyProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);

    }

    public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.MyProgressBar);
        Current_Progress=a.getInteger(
                R.styleable.MyProgressBar_CURRENT_PROGRESS,CURRENT_PROGRESS);
        Total_Progress=a.getInteger(
                R.styleable.MyProgressBar_TOTAL_PROGRESS,TOTAL_PROGRESS);
        ProgressBar_Color=a.getColor(
                R.styleable.MyProgressBar_PROGRESSBAR_COLOR,PROGRESSBAR_COLOR);
        ProgressBar_Width=(int)a.getDimension(
                R.styleable.MyProgressBar_PROGRESSBAR_WIDTH,dp2px(PROGRESSBAR_WIDTH));
        ProgressText_Color=a.getColor(
                R.styleable.MyProgressBar_PROGRESSTEXT_COLOR,PROGRESSTEXT_COLOR);
        ProgressText_SIZE=(int)a.getDimension(
                R.styleable.MyProgressBar_PROGRESSBAR_SIZE,dp2px(PROGRESSBAR_SIZE));
        Progress_Redius=(int)a.getDimension(
                R.styleable.ProgressBarWithProgress_redius,PROGRESSBAR_REDIUS);
        BackGround=a.getColor(
                R.styleable.MyProgressBar_BACKGROUND,BACKGROUND);
        //初始化画笔
        ProgressBarPaint=new Paint();
        ProgressBarPaint.setStrokeWidth(ProgressBar_Width);
        ProgressBarPaint.setColor(ProgressBar_Color);
        ProgressBarPaint.setAntiAlias(true);
        ProgressTextPaint=new Paint();
        ProgressTextPaint.setColor(ProgressText_Color);
        ProgressTextPaint.setTextSize(ProgressText_SIZE);
        ProgressTextPaint.setAntiAlias(true);
        a.recycle();

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Width=MeasureSpec.getSize(widthMeasureSpec);
        Height=MeasureSpec.getSize(heightMeasureSpec);

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setColor(BackGround);
        paint.setAntiAlias(true);
        RectF f=new RectF(Width/2-Progress_Redius,Height/2-Progress_Redius,Width/2+Progress_Redius,Height/2+Progress_Redius);
        canvas.drawArc(f,0,Current_Progress,true,ProgressBarPaint);
        canvas.drawCircle(Width/2,Height/2,Progress_Redius-10,paint);
        canvas.drawText(Current_Progress*100/360+"%",Width/2,Height/2,ProgressTextPaint);
        invalidate();
    }

    //数字转化为dp
    private int dp2px(int dp){
       return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,
               getResources().getDisplayMetrics());
    }

    public void setProgress(int progress){
        this.Current_Progress=progress;
        Log.i("onDraw","progress=="+progress);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值