Android实现计步进度的环形Progress
程序员文章站
2023-12-17 18:09:28
项目中需要实现一个计步进度的环形progress,当未达到设定目标时,绘制特定弧度((已实现步数/目标步数)*360°)的圆弧。当已实现步数大于等于目标步数时绘制整个360...
项目中需要实现一个计步进度的环形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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。