效果图,颜色可以在几改,个人感觉效果不好看
代码如下:
package com.example.mengsong.demo01; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * Created by MengSong on 2016/12/20. */ public class CustomProgress extends View { private Paint mPaint; private RectF mRectF; private float startAngle = 0; private float sweepAngle = 20; private long heightBrightPosition = 0; public CustomProgress(Context context) { super(context); } public CustomProgress(Context context, AttributeSet attrs) { super(context, attrs); } /** * 没20度画一个扇形,中间空10度的区域 * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < 12; i++) { if (i == heightBrightPosition % 12){ //滚动扇形的颜色 mPaint.setColor(Color.RED); }else { mPaint.setColor(Color.GREEN); } canvas.save(); canvas.drawArc(mRectF, startAngle, sweepAngle, false, mPaint); canvas.restore(); startAngle += 30; } heightBrightPosition++; postInvalidateDelayed(60); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int stroke = getMeasuredWidth() / 2 - 10; mRectF = new RectF(stroke / 2, stroke / 2, getMeasuredWidth() - stroke / 2, getMeasuredHeight() - stroke / 2); mPaint = new Paint(); mPaint.setDither(true); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(stroke - 15); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.GREEN); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measuredWidth = measureWidth(widthMeasureSpec); int measuredHeight = measureHeight(heightMeasureSpec); if (measuredHeight != measuredHeight) { int min = Math.min(measuredWidth, measuredHeight); setMeasuredDimension(min, min); } else { setMeasuredDimension(measuredWidth, measuredHeight); } } /** * 计算控件宽度 * * @param widthMeasureSpec * @return */ private int measureWidth(int widthMeasureSpec) { int result = 0; int mode = MeasureSpec.getMode(widthMeasureSpec); int size = MeasureSpec.getSize(widthMeasureSpec); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = 200; if (mode == MeasureSpec.AT_MOST) { result = Math.min(result, size); } } return result; } /** * 计算控件高度 * * @param heightMeasureSpec * @return */ private int measureHeight(int heightMeasureSpec) { int result = 0; int mode = MeasureSpec.getMode(heightMeasureSpec); int size = MeasureSpec.getSize(heightMeasureSpec); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = 200; if (mode == MeasureSpec.AT_MOST) { result = Math.min(result, size); } } return result; } }