自定义View——环形进度条

从  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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值