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

Android 自定義控件應用篇:打造圓形進度條

編輯:關於 Android 程式設計

一、概述

上一篇我們介紹了 Android 自定義控件的基礎知識。本篇將在此基礎上進一步實戰,通過重寫 onDraw() 方法並結合自定義屬性,實現一個可靈活配置的圓形進度條,效果如下圖:

二、實現步驟

1、編寫自定義組件 MyCircleProgress 並擴展 View

 
 public class MyCircleProgress extends View { … }

2、在 MyCircleProgress 中定義自定義屬性

通過成員變量控制圓半徑、背景色、前景色、繪制樣式、弧線寬度與最大值等,並提供線程安全的 setProgress 方法。

 
 public int progress = 0;//進度實際值,當前進度 /** * 自定義控件屬性,可靈活的設置圓形進度條的大小、顏色、類型等 */ private int mR;//圓半徑,決定圓大小 private int bgColor;//圓或弧的背景顏色 private int fgColor;//圓或弧的前景顏色,即繪制時的顏色 private int drawStyle; //繪制類型 FILL畫圓形進度條,STROKE繪制弧形進度條 private int strokeWidth;//STROKE繪制弧形的弧線的寬度 private int max;//最大值,設置進度的最大值 /** * 設置進度,此為線程安全控件,由於考慮多線的問題,需要同步 */ public synchronized void setProgress(int progress) { if(progress<0){ progress=0; }else if(progress>max){ progress=max; }else{ this.progress = progress; } } public int getMax() { return max; }

3、編寫 attrs.xml 資源文件

該文件放在 res/values 目錄下,用於聲明可配置屬性。

 
      

4、在構造函數中初始化屬性

 
 private void initProperty(AttributeSet attrs){ TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar); mR=tArray.getInteger(R.styleable.CircleProgressBar_r,10); bgColor=tArray.getColor(R.styleable.CircleProgressBar_bgColor, Color.GRAY); fgColor=tArray.getColor(R.styleable.CircleProgressBar_fgColor, Color.RED); drawStyle=tArray.getInt(R.styleable.CircleProgressBar_drawStyle, 0); strokeWidth=tArray.getInteger(R.styleable.CircleProgressBar_strokeWidth, 10); max=tArray.getInteger(R.styleable.CircleProgressBar_max, 100); } public MyCircleProgress(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); // 消除鋸齒 this.paint.setStyle(Style.STROKE); // 繪制空心圓或 空心矩形 initProperty(attrs); }

5、在布局文件中添加 MyCircleProgress 組件

 
   

6、重寫 onDraw 方法完成繪制

先畫出灰色底環,再根據 drawStyle 決定是畫實心圓還是弧形進度,通過 drawArc 按比例呈現進度。

 
 protected void onDraw(Canvas canvas) { super.onDraw(canvas); int center = getWidth() / 2; // 圓心位置 this.paint.setColor(bgColor); this.paint.setStrokeWidth(strokeWidth); canvas.drawCircle(center, center, mR, this.paint); // 繪制圓環 this.paint.setColor(fgColor); if(drawStyle==0){ this.paint.setStyle(Style.STROKE); opt=false; }else{ this.paint.setStyle(Style.FILL); opt=true; } int top = (center - mR); int bottom = (center + mR); RectF oval = new RectF(top, top, bottom, bottom); canvas.drawArc(oval, 270, 360*progress/max, opt, paint); } 

7、編寫 MainActivity 驅動進度動畫

借助 AsyncTask 在後台不斷遞增進度值,並在 onProgressUpdate 中調用 setProgress 與 invalidate 觸發重繪,實現平滑的進度動畫。

 public class MainActivity extends Activity { private MyCircleProgress progressView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressView = (MyCircleProgress) findViewById(R.id.MyCircleProgress); new ProgressAnimation().execute(); } class ProgressAnimation extends AsyncTask { @Override protected Void doInBackground(Void... params) { //進度值不斷的變化 for (int i = 0; i < progressView.getMax(); i++) { try { publishProgress(i); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } @Override protected void onProgressUpdate(Integer... values) { //更新進度值 progressView.setProgress(values[0]); progressView.invalidate(); super.onProgressUpdate(values); } } }
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved