android:渐变色进度条(圆环)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23843705/article/details/79528829
public class CircleView extends View {
    private Paint paint;//画笔
    private Paint textPaint;//文言画笔
    private RectF oval;//rectF对象(圆环)
    private int currentDegree = 90;//当前度数(除360求百分比)
    @SuppressLint("RestrictedApi")
    private ArgbEvaluator argbEvaluator = new ArgbEvaluator();//颜色渐变差值器
    private int height;//控件高
    private int width;//控件宽
    private int circleWidth;//圆环宽
    private final static int strokeWidth = 4;//画笔大小
    private boolean isGradual = true;//是否显示渐变色
    private final int GREEN = 0xff00c882;
    private final int BLUE = 0xff229fff;

    public CircleView(Context context) {
        super(context);
        //初始化画笔
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        oval = new RectF();
        paint.setStrokeWidth(strokeWidth);//线宽
        paint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(20);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //计算最小宽度
        height = View.MeasureSpec.getSize(heightMeasureSpec);
        width = View.MeasureSpec.getSize(widthMeasureSpec);

        if (width >= height) {
            circleWidth = height;
        } else {
            circleWidth = width;
        }

        setMeasuredDimension(circleWidth, circleWidth);
        oval.left = strokeWidth / 2; // 左边
        oval.top = strokeWidth / 2; // 上边
        oval.right = circleWidth - strokeWidth / 2; // 右边
        oval.bottom = circleWidth - strokeWidth / 2; // 下边
//自动旋转
//		handler.postDelayed(runnable, 500);
    }

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

        for (int i = 0; i < currentDegree; i++) {
            if (isGradual) {
                @SuppressLint("RestrictedApi") Integer color;
                if (i < 180) {
                    color = (Integer) argbEvaluator.evaluate(i / 360f, GREEN, BLUE);//颜色插值器(level 11以上才可以用)
                } else {
                    color = (Integer) argbEvaluator.evaluate(i / 360f, BLUE, GREEN);

                }
                paint.setColor(color);
            } else {
                if (i < 180) {
                    paint.setColor(Color.BLUE);//右半边颜色
                } else {
                    paint.setColor(Color.GREEN);//所半边颜色
                }
            }

            canvas.drawArc(oval, -90 + i, 1.35f, false, paint); // 绘制圆弧 1.35f是每个色块宽度

//            色块显示
//            if (i % 2 == 0) {
//                canvas.drawArc(oval, -90 + i, 1.35f, false, paint); // 绘制圆弧 1.35f是每个色块宽度
//            }
        }
        String text = (int) ((float) currentDegree / 360 * 100) + "%";
        float textLength = textPaint.measureText(text);
        int centre = getWidth() / 2;
        canvas.drawText(text, centre, centre + textPaint.getTextSize() / 2, textPaint);
    }

    /**
     * 根据百分比设置颜色范围
     *
     * @param pDegree
     */
    public void setCurrentDegree(float pDegree) {
        this.currentDegree = (int) (360f * pDegree);
    }

    /**
     * 颜色是否渐变
     *
     * @param gradual
     */
    public void setGradual(boolean gradual) {
        this.isGradual = gradual;
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (currentDegree > 360)
                currentDegree = 0;
            invalidate();
            handler.postDelayed(runnable, 500);
            currentDegree++;
        }
    };
}

猜你喜欢

转载自blog.csdn.net/qq_23843705/article/details/79528829