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; } } }
Android自定義view(圓形進度條)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.