Android实现计步进度的环形Progress
更新时间:2017年02月17日 11:56:41 作者:周木水
这篇文章主要为大家详细介绍了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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android编程经典代码集锦(复制,粘贴,浏览器调用,Toast显示,自定义Dialog等)
这篇文章主要介绍了Android编程经典代码集锦,包括Android的复制,粘贴,浏览器调用,Toast显示,自定义Dialog等实现技巧,非常简单实用,需要的朋友可以参考下2016-01-01
Android中使用IntentService创建后台服务实例
这篇文章主要介绍了Android中使用IntentService创建后台服务实例,IntentService提供了在单个后台线程运行操作的简单结构,需要的朋友可以参考下2014-06-06
Android入门之BroadCast模拟实现异地登录事件发生后的主动退出
随着对BroadCast的越来越深入,我们今天要实现一个稍微复杂一点的BroadCast。即只允许一个设备登录一个帐号时,APP会弹一个对话框如:您的账号在别处登录,请重新登陆!感兴趣的可以了解一下2022-12-12
Android 扫码枪输入时屏蔽软键盘和顶部状态栏的解决方案
在Android设备上,使用扫码枪时常遇到软键盘和顶部状态栏显示问题,本文介绍了在Android 7.1.2版本上,如何通过设置inputType为none屏蔽软键盘,以及通过hideStatusBar和NoActionBar方法隐藏顶部状态栏,以优化扫码枪使用界面,这些方法有助于提升使用扫码枪场景的用户体验2024-10-10


最新评论