Android自定义view(圆形进度条)
2017-09-15 21:04
260 查看
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,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 自定义View,自定义属性(带进度的圆形进度条)
- Android高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 自定义view --圆形百分比(进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 自定义 View 圆形进度条总结
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)