Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義圓形倒計時進度條完整實現與踩坑記錄

編輯:關於 Android 程式設計

效果預覽

源碼傳送門:https://github.com/yanzhenjie/CircleTextProgressbar

實現與原理

這種帶文字的圓形進度條在很多 App 中都能看到,比如歡迎頁倒計時、檔案下載倒計時等。有些人一看到自定義 View 就發慌,擔心要繼承 View、要繪製之類,答案確實如此,但實現起來其實很簡單。

原理分析

觀察效果,需要幾個部分組成:逐漸增減的圓形進度條、中間的展示文字、外層包裹的圓、圓心填充色,以及點擊變色所需的 ColorStateList。由於帶文字,第一個想到的就是 TextView,正好省去記錄字體顏色。剩下的進度條、輪廓圓與填充圓則需自行繪製。

CircleTextProgressbar 的特色

它支援自動倒計時、自動遞減與遞增進度。設定好屬性後呼叫 start() 即可自動倒計時;想再跑一遍就呼叫 reStart()。若不想自動,可用 setProgress() 像系統進度條一樣修改數值:

 progressBar.setProgressType(CircleTextProgressbar.ProgressType.COUNT); progressBar.setProgressLineWidth(30); progressBar.setTimeMillis(3500); progressBar.setProgressColor(Color.RED); progressBar.setOutLineColor(Color.RED); progressBar.setInCircleColor(Color.RED); progressBar.start(); progressBar.setProgress(100); 

踩坑過程:View 繪製區域

因為繼承的是 TextView,文字多時它會變長,繪製出來的圓長寬不一致,只能看到一部分或呈橢圓,所以得擴大繪製區域。我最初想到重寫 layout():

 @Override public void layout(int left, int top, int right, int bottom) { int w = right - left; int h = bottom - top; int size = w > h ? w : h; if (w > h) { bottom += (size - h); } else { right += (size - w); } super.layout(left, top, right, bottom); } 

單獨使用沒問題,但放進 LinearLayout 後多個 View 重疊了。原來父 Layout 已經指定了寬高,我強行佔用別人的空間。正解是重寫 onMeasure(),在測量時就告訴父 Layout 我要多大:

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getMeasuredWidth(); int height = getMeasuredHeight(); int size = width > height ? width : height; setMeasuredDimension(size, size); } 

繪製 View 的實現

畫圓圈要重寫 onDraw()。先準備畫筆並開啟抗鋸齒:

 Paint mPaint = new Paint(); mPaint.setAntiAlias(true);

透過 getDrawingRect(Rect) 取得繪製區域,並計算半徑:

 Rect bounds = new Rect(); @Override protected void onDraw(Canvas canvas) { getDrawingRect(bounds); int size = bounds.height() > bounds.width() ? bounds.width() : bounds.height(); float outerRadius = size / 2; } 

繪製填充圓:點擊變色需 ColorStateList,支援在 XML 定義:

 ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); int circleColor = inCircleColors.getColorForState(getDrawableState(), 0); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(circleColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth, mPaint); 

繪製外部邊框圓:空心線用 STROKE:

 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(outLineWidth); mPaint.setColor(outLineColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth / 2, mPaint); 

繪製文字:因為移除了 super.onDraw(canvas),需自行畫字並置中:

 Paint paint = getPaint(); paint.setColor(getCurrentTextColor()); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); float textY = bounds.centerY() - (paint.descent() + paint.ascent()) / 2; canvas.drawText(getText().toString(), bounds.centerX(), textY, paint); 

繪製進度條:它其實是一段圓弧,關鍵在指定繪製區域,需減去(外框線寬+進度條線寬)/2 以免覆蓋外輪廓:

 mPaint.setColor(progressLineColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(progressLineWidth); mPaint.setStrokeCap(Paint.Cap.ROUND); int deleteWidth = progressLineWidth + outLineWidth; mArcRect.set(bounds.left + deleteWidth / 2, bounds.top + deleteWidth / 2, bounds.right - deleteWidth / 2, bounds.bottom - deleteWidth / 2); canvas.drawArc(mArcRect, 0, 360 * progress / 100, false, mPaint); 

完整的 onDraw() 與 onMeasure() 可參考源碼倉庫。此外需注意相容問題:CircleTextProgressbar 在 RelativeLayout 中高度會變大導致進度條略扁,解法是不要重寫 onMeasure(),並在 XML 中直接指定相同的寬高(如皆為 50dp)。

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved