从
http://blog.csdn.net/lmj623565791/article/details/24500107 学习了环形进度条的编写,但对原文的变换圆环颜色处算法不满意,自己写了一遍并尝试修改如下:
public class RingProgressView extends View {
boolean isFinish = false;
boolean isChange = false;
int progress = 0;
int firstColor = Color.argb(255, 140, 255, 255);
int secondColor = Color.argb(255, 255, 40, 40);
public RingProgressView(Context context) {
this(context, null);
}
public RingProgressView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RingProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
new Thread() {
@Override
public void run() {
while (!isFinish) {
if (progress >= 360) {
progress = 0;
isChange = !isChange;
}
progress++;
postInvalidate();
try {
Thread.sleep(8);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = getMeasuredWidth() / 2;
int centerY = getMeasuredHeight() / 2;
int radius = getWidth() / 8;
Paint circlePaint;
Paint arcPaint;
RectF rectF;
rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setColor(isChange ? secondColor : firstColor);
circlePaint.setStrokeWidth(16);
circlePaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(centerX, centerY, radius, circlePaint);
arcPaint = new Paint();
arcPaint.setColor(isChange ? firstColor : secondColor);
arcPaint.setStrokeJoin(Paint.Join.ROUND);
arcPaint.setAntiAlias(true);
arcPaint.setStyle(Paint.Style.STROKE);
arcPaint.setStrokeWidth(17);
canvas.drawArc(rectF, -90, progress, false, arcPaint);
}
}