自定义饼状ProgressBar

最近有个项目要使用到饼状进度条:
饼状progressBar
我上百度也没有找到合适的(也许是我找的方式不对),所以就打算自定义一个,这个自定义控件的思路很简单,先绘制中心圆,然后绘制外围圆环,最后绘制扇形进度,所以也没有什么好讲的直接上代码吧。

Java代码如下:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/** 自定义饼状进度条
 * Created by chen on 2018/8/3.
 */
public class PieProgressBar extends View {

    //画笔
    private Paint paint;

    //中心圆的颜色
    private int roundColor;

    //饼状进度的颜色
    private int pieColor;

    //进度,初始值为0
    private int progress = 0;

    //最大进度
    private int maxProgress;

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

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

    public PieProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PieProgressBar);
        roundColor = typedArray.getColor(R.styleable.PieProgressBar_roundColor,
                Color.parseColor("#ffffff"));
        pieColor = typedArray.getColor(R.styleable.PieProgressBar_pieColor,
                Color.parseColor("#FD9748"));
        maxProgress = typedArray.getInt(R.styleable.PieProgressBar_maxProgress, 100);
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //整个控件的半径
        float radius = (float)(getWidth() / 2);

        //饼状进度的半径
        float pieRadius = radius - radius / 6 ;

        //绘制中心圆
        paint.setStyle(Paint.Style.FILL);   //设置是否填充
        paint.setColor(roundColor);  //设置画笔颜色
        paint.setAntiAlias(true);  //设置是否消除锯齿
        canvas.drawCircle(radius, radius, pieRadius, paint);

        //绘制外层圆环
        paint.setColor(pieColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(radius / 6);
        canvas.drawCircle(radius, radius, pieRadius, paint);

        //绘制扇形进度
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(pieColor);
        RectF rectF = new RectF(radius - pieRadius, radius - pieRadius,
                radius + pieRadius, radius + pieRadius);
        canvas.drawArc(rectF, -90, 360 * progress / maxProgress,
                true, paint);
    }

    public void setMaxProgress(int maxProgress) {
        if (maxProgress > 0) {
            this.maxProgress = maxProgress;
        }
    }

    public int getMaxProgress() {
        return maxProgress;
    }

    public void setProgress(int progress) {
        if (progress > maxProgress) {
            progress = maxProgress;
        }

        if (progress < 0) {
            progress = 0;
        }

        this.progress = progress;
        invalidate();
    }

    public int getProgress() {
        return progress;
    }
}

运行效果:
自定义饼状ProgressBar_第1张图片

你可能感兴趣的:(Android)