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° ring.
Effect diagram:
Code implementation:
Set the completed steps and the 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 progress:
public void setProgress(int progress) { this.mProgress = progress; this.invalidate(); }
Set 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 ring 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 number 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);
Tracez le nombre de pas cible :
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);
Voici la totalité du contenu de cet article, j'espère qu'il vous sera utile dans vos études, et j'espère que vous soutiendrez également le tutoriel d'alarme.
Déclaration : Le contenu de cet article est来源于网络,et appartient à ses auteurs respectifs. Le contenu est apporté par les utilisateurs d'Internet et téléchargé spontanément. Ce site ne détient pas de droits de propriété, n'a pas été édité par l'homme, et n'assume aucune responsabilité juridique connexe. Si vous trouvez du contenu suspect de violation de droits d'auteur, veuillez envoyer un e-mail à : notice#oldtoolbag.com (veuillez remplacer # par @ lors de l'envoi d'un e-mail pour signaler une violation, et fournir des preuves pertinentes. Une fois vérifié, ce site supprimera immédiatement le contenu suspect de violation de droits d'auteur.)