自定義順時針進度圓環(huán)

自定義順時針進度圓環(huán)

```

public class CirqueView extends View {

? ? /**

? ? * View默認最小寬度

? ? */

? ? private static final int DEFAULT_MIN_WIDTH = 220;

? ? /**

? ? * 圓環(huán)顏色

? ? */

? ? 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;

? ? ? ? //畫背景灰色圓環(huán)

? ? ? ? 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);

? ? ? ? //畫彩色圓環(huán)

? ? ? ? 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);

? ? ? ? //小圓環(huán)的文字的位置

? ? ? ? //畫中間數(shù)值

? ? ? ? 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);

? ? }

}

```

```

<com.pxsxy.zhihuitang.customview.CirqueView

? ? 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);

效果圖:


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容