Android自定义view(圆形进度条)

public class MyRoundProgressBar extends View {
    private Paint paint;
    float x, y;
    CircleStyle cs;
    private int rout;
    private float radius;

    public MyRoundProgressBar(Context context) {
        super(context);
        init();
    }

    public MyRoundProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyRoundProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    public void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        rout = 0;
        radius = 100;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (cs == null) {
            cs = new CircleStyle(radius, Color.BLACK, rout);
            x = getWidth() / 2;
            y = getHeight() / 2;
            paint.setColor(Color.BLACK);
            //消除锯齿
            paint.setStyle(Paint.Style.STROKE);
            //设置粗细 
            paint.setStrokeWidth(1);
            canvas.drawCircle(x, y, cs.getRadiu() - 10, paint);
            canvas.drawCircle(x, y, cs.getRadiu() + 10, paint);

            paint.setColor(cs.getColor());
            //设置粗细
            paint.setStrokeWidth(18);
            RectF rect = new RectF();
            //进行画圆弧
            rect.set(x - cs.getRadiu(), y - cs.getRadiu(), x + cs.getRadiu(), y + cs.getRadiu());
            canvas.drawArc(rect, 0.0f, cs.getRound(), false, paint);
            startAnimation();
        } else {
            paint.setColor(Color.BLACK); //设置圆环的颜色
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            canvas.drawCircle(x, y, cs.getRadiu() - 10, paint);
            canvas.drawCircle(x, y, cs.getRadiu() + 10, paint);

            paint.setColor(cs.getColor());
            paint.setStrokeWidth(18);
            RectF rect = new RectF();
            rect.set(x - cs.getRadiu(), y - cs.getRadiu(), x + cs.getRadiu(), y + cs.getRadiu());
            canvas.drawArc(rect, 0.0f, cs.getRound(), false, paint);
        }
    }

    private void startAnimation() {
        ValueAnimator animator = ValueAnimator.ofObject(new CircleEvaluator(), new CircleStyle(radius, Color.BLACK, 0), new CircleStyle(radius, Color.BLACK, 360));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                cs = (CircleStyle) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(10000);
        animator.start();
    }

    class CircleEvaluator implements TypeEvaluator {
        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            CircleStyle start = (CircleStyle) startValue;
            CircleStyle end = (CircleStyle) endValue;
            float round = start.getRound() + fraction * (end.getRound() - start.getRound());
            
            int colorR = Math.abs((int) (216 - fraction * (245 - 16)));
            int colorG = Math.abs((int) (156 - fraction * (45 - 16)));
            int colorB = Math.abs((int) (24 - fraction * (234 - 16)));

            //三原色的设置
            String tempR = Integer.toHexString(colorR);
            String tempG = Integer.toHexString(colorG);
            String tempB = Integer.toHexString(colorB);
            
            if (tempR.length() < 2) {
                tempR += "0";
            }
            if (tempG.length() < 2) {
                tempG += "0";
            }
            if (tempB.length() < 2) {
                tempB += "0";
            }

            String tempColor = "#" + tempR + tempG + tempB;//#fff3a3
            System.out.println("---" + tempColor);
            int color = Color.parseColor(tempColor);
            CircleStyle cs = new CircleStyle(radius, color, round);
            return cs;
        }
    }

    class CircleStyle {
        float radiu;
        int color;
        float round;

        public CircleStyle(float radiu, int color, float round) {
            this.radiu = radiu;
            this.color = color;
            this.round = round;
        }

        public float getRadiu() {
            return radiu;
        }

        public void setRadiu(float radiu) {
            this.radiu = radiu;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public float getRound() {
            return round;
        }

        public void setRound(float round) {
            this.round = round;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值