欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android自定义带加载动画效果的环状进度条

时间:2020-12-28 12:42:56|栏目:Android代码|点击:

最近闲来无事,自定义了一个环状进度条,话不多说直接上代码 :

public class CircleProgressView extends View{
  private Paint mCirPaint;
  private Paint mArcPaint;
  private Paint mTextPaint;
  private float radius=200;
  private int textsize=60;
  private int progress=68;
  private int stokeWidth=10;
  private int circleColor=Color.GRAY;
  private int arcColor=Color.GREEN;
  private int textColor=Color.BLACK;
  private int speed=0;
  public CircleProgressView(Context context) {
    super(context);
  }

  public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public CircleProgressView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setRadius(float radius){
    this.radius=radius;
    invalidate();
  }

  public void setTextSize(int textsize){
    this.textsize=textsize;
    invalidate();
  }

  public void setProgress(int progress){
    this.progress=progress;
  }

  public void setStokewidth(int stokeWidth){
    this.stokeWidth=stokeWidth;
    invalidate();
  }

  public void setColor(int circleColor,int arcColor,int textColor){
    this.circleColor=circleColor;
    this.arcColor=arcColor;
    this.textColor=textColor;
    invalidate();
  }

  public void setSpeed(int speed){
    this.speed=speed;
  }

  private void init() {
    mCirPaint=new Paint();
    mCirPaint.setColor(circleColor);
    mCirPaint.setAntiAlias(true);
    mCirPaint.setStyle(Paint.Style.STROKE);
    mCirPaint.setStrokeWidth(stokeWidth);

    mArcPaint=new Paint();
    mArcPaint.setColor(arcColor);
    mArcPaint.setAntiAlias(true);
    mArcPaint.setStyle(Paint.Style.STROKE);
    mArcPaint.setStrokeWidth(stokeWidth);

    mTextPaint=new Paint();
    mTextPaint.setColor(textColor);
    mTextPaint.setTextSize(textsize);
    mTextPaint.setAntiAlias(true);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    init();
    float centerX=getWidth()/2;
    float centerY=getHeight()/2;
    canvas.drawCircle(centerX,centerY,radius,mCirPaint);
    canvas.drawArc(centerX-radius,centerY-radius,centerX+radius,centerY+radius,-90,progress*360/100,false,mArcPaint);
    canvas.drawText(progress+"%",centerX-(mTextPaint.measureText(progress+"%"))/2,centerY+textsize/2,mTextPaint);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (speed!=0){
      startProgress();
    }
  }

  public void startProgress(){
    final int preProgress=progress;
    new CountDownTimer(preProgress * speed, speed) {
      @Override
      public void onTick(long l) {
        setProgress(preProgress-(int) (l/speed));
        invalidate();
      }

      @Override
      public void onFinish() {
        setProgress(preProgress);
        invalidate();
        this.cancel();
      }
    }.start();
  }

}

相关用法:
setProgress(progress);//设置进度
setRadius(300);//设置半径
setStokewidth(60);//设置环宽
setTextSize(80);//设置文字进度大小
setColor(Color.GRAY,Color.RED,Color.BLUE);//设置颜色(环的颜色,进度条的颜色,文字进度的字体颜色)
setSpeed(20);//设置动画速度,这里的数值是每次进度加一所用时间,所以数值越小动画速度越快

测试代码:

  mCircleProgressView= (CircleProgressView) findViewById(R.id.circle_progress);
    mCircleProgressView.setProgress(progress);
    mCircleProgressView.setRadius(300);
    mCircleProgressView.setStokewidth(60);
    mCircleProgressView.setTextSize(80);
    mCircleProgressView.setColor(Color.GRAY,Color.RED,Color.BLUE);
    mCircleProgressView.setSpeed(20);

测试效果

上一篇:Android Activity的启动过程源码解析

栏    目:Android代码

下一篇:Android导入现有的数据库方法示例

本文标题:Android自定义带加载动画效果的环状进度条

本文地址:http://www.codeinn.net/misctech/37685.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有