环形进度条的简单实现

一、自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CircleProgressView">
        <attr name="text" format="string"/>
        <attr name="textColor" format="color|reference"/>
        <attr name="textSzie" format="reference|dimension"/>
        <attr name="bgCircleColor" format="color|reference"/>
        <attr name="bgCircleStrokeWidth" format="dimension|reference"/>
        <attr name="progressCircleColor" format="color|reference"/>
        <attr name="progressStrokeWidth" format="dimension|reference"/>
        <attr name="maxProgress" format="integer"/>
        <attr name="progress" format="integer"/>
    </declare-styleable>

</resources>


二、java代码实现
public class CircleProgressView extends View{

    private String mText;
    private int    mTextColor;
    private  float mTextSize;

    private int     mBackgroudCircleColor;
    private float   mBackgroundCircleStrokeWidth;

    private int     mProgressCircleColor;
    private float   mProgressCircleWidth;

    private int     mMaxProgress;
    private int     mProgess;

    private Paint mPaint;
    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        mText                            = typedArray.getString(R.styleable.CircleProgressView_text);
        mTextColor                       = typedArray.getColor(R.styleable.CircleProgressView_textColor,getResources().getColor(R.color.
                colorAccent));
        mTextSize                        = typedArray.getDimension(R.styleable.CircleProgressView_textSzie, getResources().getDimension(
                R.dimen.circleProgressTextSize));
        mBackgroudCircleColor           = typedArray.getColor(R.styleable.CircleProgressView_bgCircleColor,getResources().getColor(R.
        color.greenOne));
        mBackgroundCircleStrokeWidth    = typedArray.getDimension(R.styleable.CircleProgressView_bgCircleStrokeWidth,getResources().
        getDimension(R.dimen.bgCircleProgressStrokeWidth));
        mProgressCircleColor             = typedArray.getColor(R.styleable.CircleProgressView_progressCircleColor,getResources().
                getColor(R.color.redOne));
        mProgressCircleWidth             = typedArray.getDimension(R.styleable.CircleProgressView_progressStrokeWidth,getResources().
                getDimension(R.dimen.bgCircleProgressStrokeWidth));
        mMaxProgress                      = typedArray.getInteger(R.styleable.CircleProgressView_maxProgress,100);
        mProgess                           = typedArray.getInteger(R.styleable.CircleProgressView_progress,50);
        typedArray.recycle();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int diameter = getWidth() > getHeight()? getHeight() :getWidth();
        float radius = diameter/2.0f;
        mPaint.setColor(mBackgroudCircleColor);
        mPaint.setStrokeWidth(mBackgroundCircleStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(radius,radius,radius - mBackgroundCircleStrokeWidth/2,mPaint);
        if(mProgess > mMaxProgress || mProgess < 0){
            mProgess = mMaxProgress;
        }
        mPaint.setColor(mProgressCircleColor);
        mPaint.setStrokeWidth(mProgressCircleWidth);
        RectF rectF = new RectF(0 + mBackgroundCircleStrokeWidth/2,0+ mBackgroundCircleStrokeWidth/2,
                diameter - mBackgroundCircleStrokeWidth/2,diameter - mBackgroundCircleStrokeWidth/2);
        canvas.drawArc(rectF,-90,360.0f*mProgess/mMaxProgress,false,mPaint);
        if(!TextUtils.isEmpty(mText)){
            mPaint.setTextSize(mTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            float length = mPaint.measureText(mText);
            canvas.drawText(mText,radius - length/2,radius + mTextSize/2,mPaint);
        }
    }

    public void updateProgress(int progress){
        if(mProgess != progress){
            if(0 <= progress && progress <= 100){
                mProgess = progress;
                mText = mProgess + "%";
                postInvalidate();
            }
        }
    }
}
 
 
 
 
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闽农qq:994955138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值