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

動手做一個圓形進度條:從零開始的自定義 View

編輯:關於 Android 程式設計

網路上找到的圓形進度條多半是包裝完整、功能齊全的成果,對初學者反而不好消化。這篇就一步一步把它拆開,看它到底怎麼畫出來。

相關影片:
https://edu.csdn.net/course/detail/3719/65396

先看成品:

main 版面

就是一個按鈕加一個自定義 View,非常樸素:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開始下載" android:onClick="start" /> <com.example.pb.ProgressView android:id="@+id/circleView" android:layout_width="100dp" android:layout_height="100dp" /> </LinearLayout> 

自定義 ProgressView

預設呈現圖中的第一種樣式(描邊):

 package com.example.pb; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.graphics.RectF; import android.os.Handler; import android.util.AttributeSet; import android.view.View; public class ProgressView extends View { int progress = 0; private String text="0%"; private int max = 100; public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ProgressView(Context context, AttributeSet attrs) { super(context, attrs); } public ProgressView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 對於畫筆 Paint paint = new Paint(); // 設置抗鋸齒 paint.setAntiAlias(true); // 設置畫筆顏色 // 三種樣式--Stroke 只要描邊 Fill 填充 FILL_AND_STROKE和既有描邊又有填充 paint.setStyle(Style.STROKE); //設置描邊寬度 paint.setStrokeWidth(2); //定義外圈員的顏色 paint.setColor(Color.RED); //繪制圓形進度條--獲取當前控件多大,正好讓進度條在這個控件區間內 canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint); //重新設置描邊寬度,這個寬度最好能完全蓋過圓形 paint.setStrokeWidth(3); //定義限制圓弧的矩形,當前這樣定義正好讓圓弧和圓重合 RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth()); //設置進度條(圓弧的顏色) paint.setColor(Color.GREEN); //繪制,設置進度條的度數從0開始,結束值是個變量,可以自己自由設置,來設置進度 //true和false 代表是否使用中心點,如果true,代表連接中心點,會出現扇形的效果 canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //文字的繪制 paint.setTextSize(40); //設置文字寬度 paint.setStrokeWidth(1.0f); //測量文字大小-提前准備個矩形 Rect bounds = new Rect(); //測量文字的寬和高,測量的值可以根據矩形獲取 paint.getTextBounds(text, 0, text.length(), bounds); paint.setColor(Color.BLACK); paint.setStyle(Style.FILL); //繪制文字,計算文字的寬高進行設置 canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2, getMeasuredWidth()/2 + bounds.height() / 2, paint); } /** * 初始設置當前進度的最大值-默認100 * @param max */ public void setMax(int max) { this.max = max; } /** * 更新進度和文字 * @param progress * @param text */ public void setProgressAndText(int progress, String text) { this.progress = progress; this.text = text; //重新繪制 postInvalidate(); } } 

這裡有幾個值得記住的要點:外圈用 drawCircle 畫軌道,進度用 drawArc 從 0 度掃到 360 * progress / max;drawArc 最後一個參數傳 false 才是「弧線」,傳 true 會連到圓心變成扇形。文字居中則靠 getTextBounds 先量出寬高,再從中心扣除一半。

如果想要第二種(扇形填滿)效果,只要改兩行:

 //設置填充模式 paint.setStyle(Style.FILL); //繪制,設置進度條的度數從0開始,結束值是個變量,可以自己自由設置,來設置進度 //true和false 代表是否使用中心點,如果true,代表連接中心點,會出現扇形的效果 canvas.drawArc(oval, 0, 360 * progress / max, false, paint); 

Activity 中模擬下載

 package com.example.pb; import android.app.Activity; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private ProgressView circleView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); circleView = (ProgressView) findViewById(R.id.circleView); } int progress = 0; public void start(View v) { // 1000公裡 circleView.setMax(100); progress=0; new Thread() { public void run() { while (true) { progress = progress + 1; String text = progress + "%"; circleView.setProgressAndText(progress, text); try { sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (progress == 100) { break; } } }; }.start(); } } 

最後提醒一個實務細節:這裡在子線程直接呼叫 postInvalidate() 是對的(非 UI 線程請用 postInvalidate,UI 線程才用 invalidate)。另外三個建構子務必都覆寫,少了 AttributeSet 那個,透過 XML 引用時會直接崩潰。

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