欢迎来到代码驿站!

Android代码

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

Android实现计步进度的环形Progress

时间:2021-03-01 13:43:17|栏目:Android代码|点击:

项目中需要实现一个计步进度的环形Progress,当未达到设定目标时,绘制特定弧度((已实现步数/目标步数)*360°)的圆弧。当已实现步数大于等于目标步数时绘制整个360°圆环。

效果图:

代码实现:

设置已完成步数和目标步数:

  public void setStep(int stepDone, int stepGoal) {
    this.stepDone = stepDone;
    this.stepGoal = stepGoal;
    int progess = (stepDone * 100) / stepGoal;
    if (progess > 100) {
      setProgress(100);
    } else {
      setProgress(progess);
    }
  }

设置进度:

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

设置画笔属性:

mPaint.setAntiAlias(true);
mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
canvas.drawColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(LINE_WIDTH_BG);
mPaint.setStyle(Paint.Style.STROKE);

绘制环形和背景:

canvas.drawArc(mRectF, -90, 360, false, mPaint);
mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30));
canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);

绘制步数和单位:

mPaint.setStrokeWidth(TEXT_WIDTH);
    String text = stepDone + context.getString(R.string.step_unit);
    int textHeight = height / 4;
    mPaint.setTextSize(textHeight);
    int textWidth = (int) mPaint.measureText(text, 0, text.length());
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 4, mPaint);

绘制目标步数:

 String textGoal = "/" + stepGoal;
    int textGoalHeight = height / 8;
    mPaint.setTextSize(textGoalHeight);
    int textGoalWidth = (int) mPaint.measureText(textGoal, 0, textGoal.length());
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawText(textGoal, width / 2 - textGoalWidth / 2, height / 2 + textHeight / 2
        + textGoalHeight, mPaint);

上一篇:Android流式布局FlowLayout详解

栏    目:Android代码

下一篇:Android实现微信自动抢红包的程序

本文标题:Android实现计步进度的环形Progress

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

推荐教程

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

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

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

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

Copyright © 2020 代码驿站 版权所有