给progress 进度条添加动画效果

/**
 * 设置百分比
 *
 * @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();


}


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值