自定义顺时针进度圆环

自定义顺时针进度圆环

```

public class CirqueView extends View {

    /**

    * View默认最小宽度

    */

    private static final int DEFAULT_MIN_WIDTH = 220;

    /**

    * 圆环颜色

    */

    private int[] doughnutColors = new int[]{Color.parseColor("#f2b938"), Color.parseColor("#edd228"), Color.parseColor("#efc730"), Color.parseColor("#f1bc37"), Color.parseColor("#f4ac41"), Color.parseColor("#f79e4a"),

            Color.parseColor("#f99350"), Color.parseColor("#fc8858"), Color.parseColor("#fe7e5e"), Color.parseColor("#f6a545"), Color.parseColor("#f6a545"), Color.parseColor("#f2b938")};

    private int width;

    private int height;

    private float currentValue = 0f;

    private Paint paint = new Paint();

    public CirqueView(Context context) {

        super(context);

    }

    public CirqueView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

    public CirqueView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }

    private void resetParams() {

        width = getWidth();

        height = getHeight();

    }

    private void initPaint() {

        paint.reset();

        paint.setAntiAlias(true);

    }

    //需要展示的来源

    //展示去提升

    public void setValue(float value) {

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(currentValue, value);

        valueAnimator.setDuration(1000);

        valueAnimator.setInterpolator(new Interpolator() {

            @Override

            public float getInterpolation(float v) {

                return 1 - (1 - v) * (1 - v) * (1 - v);

            }

        });

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override

            public void onAnimationUpdate(ValueAnimator valueAnimator) {

                currentValue = (float) valueAnimator.getAnimatedValue();

                invalidate();

            }

        });

        valueAnimator.start();

    }

    @Override

    protected void onDraw(Canvas canvas) {

        resetParams();

        float progressMax = 360f;

        //画背景灰色圆环

        initPaint();

        canvas.rotate(-90, width / 2, height / 2);

        float doughnutWidth = dip2px(getContext(), 12);

        float bgStrokeWidth = dip2px(getContext(), 6);

        paint.setStrokeWidth(bgStrokeWidth);

        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(Color.parseColor("#FFEEEB"));

        paint.setAntiAlias(true);

        paint.setDither(true);

        RectF rectF = new RectF((width > height ? Math.abs(width - height) / 2 : 0) + doughnutWidth / 2, (height > width ? Math.abs(height - width) / 2 : 0) + doughnutWidth / 2, width - (width > height ? Math.abs(width - height) / 2 : 0) - doughnutWidth / 2, height - (height > width ? Math.abs(height - width) / 2 : 0) - doughnutWidth / 2);

        canvas.drawArc(rectF, 0, progressMax, false, paint);

        //画彩色圆环

        initPaint();

        canvas.rotate(0, width / 2, height / 2);

        paint.setStrokeWidth(doughnutWidth);

        paint.setStyle(Paint.Style.STROKE);

        paint.setDither(true);

        paint.setStrokeCap(Paint.Cap.ROUND);

        if (doughnutColors.length > 1) {

            paint.setShader(new SweepGradient(width / 2, height / 2, doughnutColors, null));

        } else {

            paint.setColor(doughnutColors[0]);

        }

        paint.setMaskFilter(new BlurMaskFilter(60, BlurMaskFilter.Blur.NORMAL));

        canvas.drawArc(rectF, 0, currentValue, false, paint);

        //小圆环的文字的位置

        //画中间数值

        canvas.rotate(90, width / 2, height / 2);

        initPaint();

        paint.setColor(Color.parseColor("#FF7862"));

        paint.setTextSize(sp2px(getContext(), 35));

        paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        paint.setTextAlign(Paint.Align.CENTER);

        float baseLine = height / 2 - (paint.getFontMetrics().descent + paint.getFontMetrics().ascent) / 2;

        canvas.drawText(Math.round((currentValue / progressMax * 100f)) + "%", width / 2, baseLine, paint);

    }

    @Override

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w, h, oldw, oldh);

        RadialGradient mRadialGradient = new RadialGradient(width, height, 100, Color.parseColor("#FD6164"), Color.parseColor("#FD6164"), Shader.TileMode.REPEAT);

        paint.setShader(mRadialGradient);

    }

    /**

    * 当布局为wrap_content时设置默认长宽

    *

    * @param widthMeasureSpec

    * @param heightMeasureSpec

    */

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension(measure(widthMeasureSpec), measure(heightMeasureSpec));

    }

    private int measure(int origin) {

        int result = DEFAULT_MIN_WIDTH;

        int specMode = MeasureSpec.getMode(origin);

        int specSize = MeasureSpec.getSize(origin);

        if (specMode == MeasureSpec.EXACTLY) {

            result = specSize;

        } else {

            if (specMode == MeasureSpec.AT_MOST) {

                result = Math.min(result, specSize);

            }

        }

        return result;

    }

    private int sp2px(Context context, float spValue) {

        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;

        return (int) (spValue * fontScale + 0.5f);

    }

    private int dip2px(Context context, float dpValue) {

        final float scale = context.getResources().getDisplayMetrics().density;

        return (int) (dpValue * scale + 0.5f);

    }

}

```

```

    android:id="@+id/weekly3_cirqueview"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginLeft="@dimen/x893"

    android:layout_marginTop="@dimen/y116" />

```

如何调用?

String defeat=“92”;

cvWeekly.setValue((Float.parseFloat(defeat) / 100f) * 360);

效果图:


你可能感兴趣的:(自定义顺时针进度圆环)