English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this project, a circular progress bar for step counting needs to be implemented. When the target is not reached, draw a specific arc (the completed steps/target steps)*360°) arc. When the completed steps are greater than or equal to the target steps, draw the entire360° circle.
Effect diagram:
Code implementation:
Set the completed steps and target steps:
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); } }
Set the progress:
public void setProgress(int progress) { this.mProgress = progress; this.invalidate(); }
Set the pen properties:
mPaint.setAntiAlias(true); mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9)); canvas.drawColor(Color.TRANSPARENT); mPaint.setStrokeWidth(LINE_WIDTH_BG); mPaint.setStyle(Paint.Style.STROKE);
Draw the circular and background:
canvas.drawArc(mRectF, -9, 360, false, mPaint); mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30)); canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);
Draw the step count and unit:
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);
Draw the target step count:
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);
That's all for this article. I hope it will be helpful to everyone's study and that everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo人工 editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)