編輯:關於 Android 程式設計
效果預覽

源碼傳送門:https://github.com/yanzhenjie/CircleTextProgressbar
這種帶文字的圓形進度條在很多 App 中都能看到,比如歡迎頁倒計時、檔案下載倒計時等。有些人一看到自定義 View 就發慌,擔心要繼承 View、要繪製之類,答案確實如此,但實現起來其實很簡單。
觀察效果,需要幾個部分組成:逐漸增減的圓形進度條、中間的展示文字、外層包裹的圓、圓心填充色,以及點擊變色所需的 ColorStateList。由於帶文字,第一個想到的就是 TextView,正好省去記錄字體顏色。剩下的進度條、輪廓圓與填充圓則需自行繪製。
它支援自動倒計時、自動遞減與遞增進度。設定好屬性後呼叫 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);
因為繼承的是 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); } 畫圓圈要重寫 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)。
以上就是本文的全部內容,希望對大家的學習有所幫助。
我的 Android 進階之旅:Button 上的英文字串自動變大寫,怎麼解?
今天碰到一個關於 Button 的問題:Button 上的英文字串會自動變成大寫,執行環境是 Android 5.1。 圖 1:Button 圖 2:TextView
避免 Android 手機中毒的五個原則
自從 2009 年第一個 Android 手機病毒出現以來,網路上的惡意程式與木馬就沒有停過。以 2011 年為例,全年新增的手機惡意軟體與木馬達 8714 個,受影響的
Android 通過 ksoap2 這個框架調用 webservice 大講堂
昨天有人問我 Android 怎麼連接 mysql 數據庫、和對數據庫的操作呀。我想著給他說說 json 通信,可是他並不知道怎麼弄,哎算了吧,直接叫他用 ksoap 吧
自定義控件實戰:在 Android 中手繪圖片驗證碼
自繪控件的內容全靠代碼畫出來,整體流程大致是這樣三步:1. 定義一個類繼承 View 1. 用 TypedArray 讀取自定義屬性。View 構造方法裡有一