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

Android 自訂圓形進度條實作:從繪製到動畫

編輯:關於 Android 程式設計

在實際專案中,為了滿足客戶需求與提升使用體驗,幾乎一定會需要自訂 View。本文以圓形進度條為例,拆解自訂 View 的完整繪製流程。

整體思路:三個部分

一個圓形進度條可以拆成三塊:內圓、外圓、文字。其中內圓與文字的繪製相對單純,真正決定進度變化的是外圓,所以核心就在於怎麼畫出那段弧線。

一、定義三支畫筆

兩個 Paint 加一個 TextPaint:

 mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setStrokeWidth(CIRCLE_LINE_WIDTH); mCirclePaint.setStyle(Paint.Style.STROKE); mCirclePaint.setColor(ContextCompat.getColor(context, R.color.circle_color)); mCircleInnerPaint = new Paint(); mCircleInnerPaint.setAntiAlias(true); mCircleInnerPaint.setStyle(Paint.Style.FILL); mCircleInnerPaint.setColor(ContextCompat.getColor(context, R.color.circle_inner_color)); mTextPaint = new TextPaint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setColor(ContextCompat.getColor(context, R.color.circle_text_color)); mTextPaint.setTextSize(TEXT_SIZE); 

這裡有幾個關鍵設定值得留意:外圓用 Paint.Style.STROKE(只畫邊框),內圓用 FILL(填滿),而 setAntiAlias(true) 是讓圓弧邊緣不鋸齒的必要開關。

二、取得自訂 View 的寬高

 float halfWidth = getMeasuredWidth() / 2;
float halfHeight = getMeasuredHeight() / 2;

三、繪製外圓

 canvas.drawCircle(halfWidth, halfHeight, CIRCLE_RADIUS, mCirclePaint);

四、繪製內圓

 canvas.drawCircle(halfWidth, halfHeight, CIRCLE_RADIUS - CIRCLE_LINE_WIDTH / 2, mCircleInnerPaint);

半徑減去半個線寬,是為了讓內圓剛好填滿外圓的內側邊界,不留白縫也不會蓋掉外框。

五、繪製文字

 canvas.drawText(mProgressText, halfWidth - mTextPaint.measureText(mProgressText) / 2, halfHeight - (mTextPaint.ascent() + mTextPaint.descent()) / 2, mTextPaint);

文字置中不能單純用 halfHeight 減半個字高,因為 ascent 是負值、descent 是正值,兩者相加再除以二才是真正的視覺中心偏移量。這是很多人自製進度條時文字總是偏上的原因。

完成後的基本圖形如下:

六、加入動畫效果

進度是即時變化的,所以需要不斷更新並重繪,進度本身可以用圓弧來呈現。

設定進度的方法

 public void setProgress(float progress) { if (progress > 100) { progress = 100; } if (progress < 0) { progress = 0; } mProgress = progress; mProgressText = "Pause"; mStartProgress = true; postInvalidate(); } 

邊界檢查很重要,傳入值超出範圍會讓 drawArc 掃過超過 360 度,畫出奇怪的圖形。postInvalidate() 則是通知系統重繪。

七、在 Activity 中模擬網路請求推進度

每 30 毫秒更新一次,超過 100 就停止刷新:

 private void startProgress() { new Thread() { @Override public void run() { super.run(); float currentProgress = mCustomView.getCurrentProgress(); ++currentProgress; mCustomView.setProgress(currentProgress); try { sleep(30); if (currentProgress <= 100) { startProgress(); } else { mCustomView.progressFinished(); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } 

八、核心:依進度重繪圓弧

 float halfWidth = getMeasuredWidth() / 2; float halfHeight = getMeasuredHeight() / 2; if (null == mCircleRectF) { mCircleRectF = new RectF(halfWidth - CIRCLE_RADIUS, halfHeight - CIRCLE_RADIUS, halfWidth + CIRCLE_RADIUS, halfHeight + CIRCLE_RADIUS); } if (mStartProgress) { float swipeProgress = mProgress / 100f * 360; LogUtils.e("swipeProgress = " + swipeProgress); canvas.drawArc(mCircleRectF, -90, swipeProgress, true, mCirclePaint); } else { canvas.drawCircle(halfWidth, halfHeight, CIRCLE_RADIUS, mCirclePaint); } 

思路就是把百分比換算成角度,然後持續重繪。這裡有個容易忽略的細節:起始角寫成 -90,也就是從時鐘的 12 點方向開始順時針掃描,而不是 drawArc 預設的 3 點方向。若進度已完成或尚未開始,則直接畫一個完整圓形即可。

大體思路就是這樣,最後附上兩張效果圖:

如果有更好的實作思路,歡迎一起討論學習。

感謝閱讀,希望對大家有所幫助。

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