/** * 设置百分比 * * @param progress */ public void setProgress(int progress, boolean animal) { if (progress < 0 || progress > 100) { throw new IllegalArgumentException("progress 不可以小于0 或大于100"); } this.progress = progress; // 进度 this.animal = animal; // true 为开启动画 fase 为取消动画效果 postInvalidate(); if (progressChangedListener != null) { progressChangedListener.onProgressChanged(this, progress); } }
@Override public void draw(Canvas canvas) { Log.d(TAG, "[draw] .. in .. "); super.draw(canvas); if (animal) { // 在onDraw 里面开始执行 startAnimation(progress); animal = false; } else { linePaint = new Paint(); linePaint.setAntiAlias(true); linePaint.setStyle(Paint.Style.FILL); linePaint.setStrokeWidth(lineHeight); linePaint.setColor(getResources().getColor(lineClor)); //因为是以画布Canvas 为draw对象,所以RectF构造函数内的参数是以canvas为边界,而不是屏幕 canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, linePaint); pointPaint = new Paint(); pointPaint.setAntiAlias(true); pointPaint.setStyle(Paint.Style.FILL); pointPaint.setColor(getResources().getColor(pointColor1)); canvas.drawCircle(getCx(), getHeight() / 2, pointRadius, pointPaint); linePaintOut = new Paint(); linePaintOut.setAntiAlias(true); linePaintOut.setStyle(Paint.Style.FILL); linePaintOut.setStrokeWidth(lineHeight); linePaintOut.setColor(getResources().getColor(pointColor)); canvas.drawLine(0, getHeight() / 2, getCx(), getHeight() / 2, linePaintOut); } }
/** * 开始动画 * * @param progress * @author hubing */ private void startAnimation(int progress) { // 初始化动画 if (progressAnimator == null) { progressAnimator = new ValueAnimator(); progressAnimator.setDuration(800); progressAnimator.addUpdateListener(this); } // 重置动画值 progressAnimator.setIntValues(0, progress); progressAnimator.start(); }