項目中需要實現一個計步進度的環形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);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答