2种方式实现带进度的圆形进度条


 

progressbar默认为水平和圆形进度条,但圆形的进度条是没有进度的。下面提供2中方式实现带进度的圆形进度条。

 

1、修改progressbar的默认样式。

 

drawable/circular_progress_bar.xml

 


    


dimems.xml



    
    3
    10


 

第二种方式:继承progressbarbar,然后自己画圆弧的方式。。

 


attrs.xml

 
    
        
        
        
    


package com.test.maria.widget;

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


public class RoundProgressBar extends ProgressBar {
    private static final String TAG = "RoundProgressBar";

    private static final int DEFAULT_TEXT_COLOR = Color.RED;
    private static final float DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2f;
    private static final float DEFAULT_HEIGHT_RADIUS = 22 / 2f;

    private float mRadius = dp2px(DEFAULT_HEIGHT_RADIUS);
    private float mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
    private int mReachedBarColor;

    private Paint mPaint;

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

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

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setHorizontalScrollBarEnabled(true);
        this.setIndeterminate(false);

        initAttrs(attrs);
        initPaint();
    }

    private void initAttrs(AttributeSet attrs) {
        final TypedArray attributes = getContext().obtainStyledAttributes(attrs,
                R.styleable.RoundProgressBarWidthNumber);
        mReachedBarColor = attributes.getColor(R.styleable.RoundProgressBarWidthNumber_progress_reached_color,
                DEFAULT_TEXT_COLOR);
        mReachedProgressBarHeight = (int) attributes.getDimension(
                R.styleable.RoundProgressBarWidthNumber_progress_reached_bar_height, mReachedProgressBarHeight);
        mRadius = (int) attributes.getDimension(R.styleable.RoundProgressBarWidthNumber_radius, mRadius);
        attributes.recycle();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(mReachedBarColor);
        mPaint.setStrokeCap(Cap.BUTT);
        mPaint.setStrokeWidth(mReachedProgressBarHeight);
    }

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

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        float paintWidth = mReachedProgressBarHeight;
        // 如果不是 指定的长宽/fillparent
        if (heightMode != MeasureSpec.EXACTLY) {
            int exceptHeight = (int) (getPaddingTop() + getPaddingBottom() + mRadius * 2 + paintWidth);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight, MeasureSpec.EXACTLY);
        }
        if (widthMode != MeasureSpec.EXACTLY) {
            int exceptWidth = (int) (getPaddingLeft() + getPaddingRight() + mRadius * 2 + paintWidth);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(exceptWidth, MeasureSpec.EXACTLY);
        }
        super.onMeasure(heightMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;
        canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 270, sweepAngle, false, mPaint);
        canvas.restore();
    }

}


 附上效果图

2种方式实现带进度的圆形进度条_第1张图片

你可能感兴趣的:(android)