栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现计步进度的环形Progress

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现计步进度的环形Progress

项目中需要实现一个计步进度的环形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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/159754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号