android ProgressBar 常用圆形进度条

使用

<RoundProgressBar
	app:radius="17dp" //角度
	app:line_width="4dp"  //线条粗细
	app:color="#f3b024"   //上面颜色
	app:bg_color="#0085e3"  //底背景颜色
	/>
  1. 在 attrs文件添加
	<declare-styleable name="RoundProgressBar">
        <attr name="color" format="color"></attr>
        <attr name="bg_color" format="color"></attr>
        <attr name="line_width" format="dimension"></attr>
        <attr name="radius" format="dimension"></attr>
        <attr name="android:progress" ></attr>
	</declare-styleable>`

2.java文件

public class RoundProgressBar extends View {

    private Paint mBackPaint;
    private Paint mFrontPaint;
    private float mLineWidth;
    private int mRadius;
    private RectF mRect;
    private int mProgress;
    private int mMax = 100;

    private int mColor;
    private int mBgColor;


    public RoundProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        mRadius = (int) ta.getDimension(R.styleable.RoundProgressBar_radius, dp2px(30));
        mColor = ta.getColor(R.styleable.RoundProgressBar_color, 0xffff0000);
        mBgColor = ta.getColor(R.styleable.RoundProgressBar_bg_color, 0xffff0000);
        mLineWidth = (int) ta.getDimension(R.styleable.RoundProgressBar_line_width, dp2px(3));
        mProgress = ta.getInt(R.styleable.RoundProgressBar_android_progress, 30);

        ta.recycle();
        init();
    }

    private float dp2px(int dpVal)
    {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }


    //完成相关参数初始化
    private void init() {

        mBackPaint = new Paint();
        mBackPaint.setColor(mColor);
        mBackPaint.setAntiAlias(true);
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mLineWidth);

        mFrontPaint = new Paint();
        mFrontPaint.setColor(mBgColor);
        mFrontPaint.setAntiAlias(true);
        mFrontPaint.setStyle(Paint.Style.STROKE);
        mFrontPaint.setStrokeWidth(mLineWidth);

    }


    //标准写法
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int width = 0;
        if (widthMode == MeasureSpec.EXACTLY)
        {
            width = widthSize;
        } else
        {
            int needWidth = measureWidth() + getPaddingLeft() + getPaddingRight();
            if (widthMode == MeasureSpec.AT_MOST)
            {
                width = Math.min(needWidth, widthSize);
            } else
            {
                width = needWidth;
            }
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;

        if (heightMode == MeasureSpec.EXACTLY)
        {
            height = heightSize;
        } else
        {
            int needHeight = measureHeight() + getPaddingTop() + getPaddingBottom();
            if (heightMode == MeasureSpec.AT_MOST)
            {
                height = Math.min(needHeight, heightSize);
            } else //MeasureSpec.UNSPECIFIED
            {
                height = needHeight;
            }
        }
        setMeasuredDimension(width, height);

    }

    private int measureHeight()
    {
        return mRadius * 2;
    }

    private int measureWidth()
    {
        return mRadius * 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        initRect();
        int width = getWidth();
        int height = getHeight();
        float angle = mProgress / (float) mMax * -360;
        canvas.drawCircle(width / 2, height / 2, mRadius, mBackPaint);
        canvas.drawArc(mRect, -90, angle, false, mFrontPaint);
    }


    private void initRect() {
        int width = getWidth();
        int height = getHeight();
        if (mRect == null) {
            mRect = new RectF();
            int viewSize = (int) (mRadius * 2);
            int left = (width - viewSize) / 2;
            int top = (height - viewSize) / 2;
            int right = left + viewSize;
            int bottom = top + viewSize;
            mRect.set(left, top, right, bottom);
        }
    }

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

    public int getProgress() {
        return mProgress;
    }

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值