Android 高手進階,自訂圓形進度條,android高手進階
背景介紹
在Android 開發中,我們經常遇到各種各樣絢麗的控制項,所以,依靠我們Android本身所帶的控制項是遠遠不夠的,很多時候需要我們自己定義控制項,在開發的過程中,我們公司遇到了一種需要自己寫的一個自訂帶進度的圓形進度條,看起來非常的絢麗,當然還有一些其他的,比如:水紋形的圓形進度條等效果都是非常nice的。如果哪位朋友有實現,希望分享出來,我也好學習學習。好了多的不說,接下來,我們就來看看來如何?圓形進度條。
原文地址:http://blog.csdn.net/xiaanming/article/details/10298163
一:先上
二:執行個體代碼1.自訂屬性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color" /> <attr name="roundProgressColor" format="color" /> <attr name="roundWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable></resources>
ps:自訂屬性呢,其實大家或許不是很明白,有些小夥伴們或許知道,我這樣用,可以調用它的一個值,可以擷取值,或者說賦予值,比如:你用Android 系統本身所帶的控制項,textview其他等,你知道他有height,width,textsize,textcolor等,這些屬性,你能賦值,但是有去理解過怎樣去擷取值麼,如果不瞭解,沒關係,我這有一篇鴻洋寫的比較詳細,推薦給你:http://blog.csdn.net/lmj623565791/article/details/45022631,還在等什麼,趕緊戳進去,帶你裝逼帶你飛。
2.自訂控制項源碼1:
package com.example.testdemo.view;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.View;import com.example.testdemo.R;/** * 仿iphone帶進度的進度條,安全執行緒的View,可直接線上程中更新進度 * * @author zengtao 2015年5月12日下午7:43:32 * * */@SuppressLint("DrawAllocation")public class RoundProgressBar extends View {/** * 畫筆對象的引用 */private Paint paint;/** * 圓環的顏色 */private int roundColor;/** * 圓環進度的顏色 */private int roundProgressColor;/** * 中間進度百分比的字串的顏色 */private int textColor;/** * 中間進度百分比的字串的字型 */private float textSize;/** * 圓環的寬度 */private float roundWidth;/** * 最大進度 */private int max;/** * 當前進度 */private int progress;/** * 是否顯示中間的進度 */private boolean textIsDisplayable;/** * 進度的風格,實心或者空心 */private int style;public static final int STROKE = 0;public static final int FILL = 1;public RoundProgressBar(Context context) {this(context, null);}public RoundProgressBar(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);paint = new Paint();TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);// 擷取自訂屬性和預設值roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(228, 232, 237));roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.rgb(216, 6, 7));textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.rgb(216, 6, 7));textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 18);roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 3);max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);mTypedArray.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);/** * 1.畫最外層的大圓環 */int centre = getWidth() / 2; // 擷取圓心的x座標int radius = (int) (centre - roundWidth / 2); // 圓環的半徑paint.setColor(roundColor); // 設定圓環的顏色paint.setStyle(Paint.Style.STROKE); // 設定空心paint.setStrokeWidth(roundWidth); // 設定圓環的寬度paint.setAntiAlias(true); // 消除鋸齒canvas.drawCircle(centre, centre, radius, paint); // 畫出圓環/** * 2.畫進度百分比 */paint.setStrokeWidth(0);paint.setColor(textColor);paint.setTextSize(textSize);paint.setTypeface(Typeface.DEFAULT); // 設定字型// 中間的進度百分比,先轉換成float在進行除法運算,不然都為0int percent = (int) (((float) progress / (float) max) * 100);float textWidth = paint.measureText(percent + "%"); // 測量字型寬度,我們需要根據字型的寬度設定在圓環中間if (textIsDisplayable && style == STROKE) { canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比 }/** * 3.畫圓弧 ,畫圓環的進度 */// 設定進度是實心還是空心paint.setStrokeWidth(roundWidth); // 設定圓環的寬度paint.setColor(roundProgressColor); // 設定進度的顏色RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用於定義的圓弧的形狀和大小的界限switch (style) {case STROKE: {paint.setStyle(Paint.Style.STROKE);canvas.drawArc(oval, -90, 360 * progress / max, false, paint); // 根據進度畫圓弧break;}case FILL: {paint.setStyle(Paint.Style.FILL_AND_STROKE);if (progress != 0)canvas.drawArc(oval, -90, 360 * progress / max, true, paint); // 根據進度畫圓弧break;}}}public synchronized int getMax() {return max;}/** * 設定進度的最大值 * * @param max */public synchronized void setMax(int max) {if (max < 0) {throw new IllegalArgumentException("max not less than 0");}this.max = max;}/** * 擷取進度.需要同步 * * @return */public synchronized int getProgress() {return progress;}/** * 設定進度,此為安全執行緒控制項,由於考慮多線的問題,需要同步 重新整理介面調用postInvalidate()能在非UI線程重新整理 * * @param progress */public synchronized void setProgress(int progress) {if (progress < 0) {throw new IllegalArgumentException("progress not less than 0");}if (progress > max) {progress = max;}if (progress <= max) {this.progress = progress;postInvalidate();}}public int getCricleColor() {return roundColor;}public void setCricleColor(int cricleColor) {this.roundColor = cricleColor;}public int getCricleProgressColor() {return roundProgressColor;}public void setCricleProgressColor(int cricleProgressColor) {this.roundProgressColor = cricleProgressColor;}public int getTextColor() {return textColor;}public void setTextColor(int textColor) {this.textColor = textColor;}public float getTextSize() {return textSize;}public void setTextSize(float textSize) {this.textSize = textSize;}public float getRoundWidth() {return roundWidth;}public void setRoundWidth(float roundWidth) {this.roundWidth = roundWidth;}public int getStyle() {return style;}public void setStyle(int style) {this.style = style;}}
源碼2:
package com.example.testdemo.view;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.View;import com.example.testdemo.R;/** * 仿iphone帶進度的進度條,安全執行緒的View,可直接線上程中更新進度 * * @author zengtao 2015年5月12日下午7:43:32 * * */@SuppressLint("DrawAllocation")public class RoundProgressBar2 extends View {/** * 畫筆對象的引用 */private Paint paint;/** * 圓環的顏色 */private int roundColor;/** * 圓環進度的顏色 */private int roundProgressColor;/** * 中間進度百分比的字串的顏色 */private int textColor;/** * 中間進度百分比的字串的字型 */private float textSize;/** * 圓環的寬度 */private float roundWidth;/** * 最大進度 */private int max;/** * 當前進度 */private int progress;/** * 是否顯示中間的進度 */private boolean textIsDisplayable;/** * 進度的風格,實心或者空心 */private int style;public static final int STROKE = 0;public static final int FILL = 1;public RoundProgressBar2(Context context) {this(context, null);}public RoundProgressBar2(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundProgressBar2(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);paint = new Paint();TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);// 擷取自訂屬性和預設值roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(228, 232, 237));roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.rgb(216, 6, 7));textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.rgb(216, 6, 7));textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 18);roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 3);max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);mTypedArray.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);/** * 畫最外層的大圓環 */int centre = getWidth() / 2; // 擷取圓心的x座標int radius = (int) (centre - roundWidth / 2); // 圓環的半徑paint.setColor(roundColor); // 設定圓環的顏色paint.setStyle(Paint.Style.STROKE); // 設定空心paint.setStrokeWidth(roundWidth); // 設定圓環的寬度paint.setAntiAlias(true); // 消除鋸齒canvas.drawCircle(centre, centre, radius, paint); // 畫出圓環/** * 畫進度百分比 */paint.setStrokeWidth(0);paint.setColor(textColor);paint.setTextSize(textSize);paint.setTypeface(Typeface.DEFAULT); // 設定字型// int percent = (int) (((float) progress / (float) max) * 100); //// 中間的進度百分比,先轉換成float在進行除法運算,不然都為0float textWidth = paint.measureText("搶"); // 測量字型寬度,我們需要根據字型的寬度設定在圓環中間if (textIsDisplayable && style == STROKE) {canvas.drawText("搶", centre - textWidth / 2, centre + textSize / 2 - 4, paint); // 畫出進度百分比}/** * 畫圓弧 ,畫圓環的進度 */// 設定進度是實心還是空心paint.setStrokeWidth(roundWidth); // 設定圓環的寬度paint.setColor(roundProgressColor); // 設定進度的顏色RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用於定義的圓弧的形狀和大小的界限switch (style) {case STROKE: {paint.setStyle(Paint.Style.STROKE);canvas.drawArc(oval, -90, 360 * progress / max, false, paint); // 根據進度畫圓弧break;}case FILL: {paint.setStyle(Paint.Style.FILL_AND_STROKE);if (progress != 0)canvas.drawArc(oval, -90, 360 * progress / max, true, paint); // 根據進度畫圓弧break;}}}public synchronized int getMax() {return max;}/** * 設定進度的最大值 * * @param max */public synchronized void setMax(int max) {if (max < 0) {throw new IllegalArgumentException("max not less than 0");}this.max = max;}/** * 擷取進度.需要同步 * * @return */public synchronized int getProgress() {return progress;}/** * 設定進度,此為安全執行緒控制項,由於考慮多線的問題,需要同步 重新整理介面調用postInvalidate()能在非UI線程重新整理 * * @param progress */public synchronized void setProgress(int progress) {if (progress < 0) {throw new IllegalArgumentException("progress not less than 0");}if (progress > max) {progress = max;}if (progress <= max) {this.progress = progress;postInvalidate();}}public int getCricleColor() {return roundColor;}public void setCricleColor(int cricleColor) {this.roundColor = cricleColor;}public int getCricleProgressColor() {return roundProgressColor;}public void setCricleProgressColor(int cricleProgressColor) {this.roundProgressColor = cricleProgressColor;}public int getTextColor() {return textColor;}public void setTextColor(int textColor) {this.textColor = textColor;}public float getTextSize() {return textSize;}public void setTextSize(float textSize) {this.textSize = textSize;}public float getRoundWidth() {return roundWidth;}public void setRoundWidth(float roundWidth) {this.roundWidth = roundWidth;}}
ps:以上兩份自訂來源碼,其實你仔細看,差別不是很大,就改變了一個地方,這個更改的地方,其實是在我們現在的項目中所遇到的,所以,發不這個圓形控制項,我想你以後會用到的,值得收藏。
三.具體調用
package com.example.testdemo;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.testdemo.view.RoundProgressBar;import com.example.testdemo.view.RoundProgressBar2;/** * 主介面 * @author zengtao 2015年6月10日 下午4:02:13 * */public class MainActivity extends Activity {private RoundProgressBar2 r3;private RoundProgressBar r1, r2, r4, r5;private int pro1 = 80; // 想要顯示的進度值:如項目中,從伺服器返回的資料private int progress = 0;private boolean flag = false;private MyThread thread1;private MyThread2 thread2;private Button start;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setRoundAttribute(); // 可設定可不設定}/** * 開始方法 */private void start() {// 線程1thread1 = new MyThread();thread1.start();// 線程2thread2 = new MyThread2();thread2.start();}/** * 初始化控制項 */private void initView() {r1 = (RoundProgressBar) findViewById(R.id.prpgress1);r2 = (RoundProgressBar) findViewById(R.id.prpgress2);r3 = (RoundProgressBar2) findViewById(R.id.prpgress3);r4 = (RoundProgressBar) findViewById(R.id.prpgress4);r5 = (RoundProgressBar) findViewById(R.id.prpgress5);start = (Button) findViewById(R.id.start);start.setOnClickListener(listener);}/** * 設定圓的屬性 */private void setRoundAttribute() {r1.setRoundWidth(10);r1.setTextColor(Color.parseColor("#00ff00"));r1.setCricleColor(Color.parseColor("#ff0000"));r2.setRoundWidth(20);r2.setTextColor(Color.parseColor("#0000ff"));r2.setCricleColor(Color.parseColor("#ff00ff"));r4.setRoundWidth(20);r4.setTextColor(Color.parseColor("#ff00ff"));r4.setCricleColor(Color.parseColor("#ff0000"));r4.setStyle(0);r4.setRoundWidth(20);r4.setTextColor(Color.parseColor("#000000"));r4.setCricleColor(Color.parseColor("#ffff00"));r4.setStyle(1);}/** * 點擊事件 */OnClickListener listener = new OnClickListener() {@Overridepublic void onClick(View v) {if (v == start) {start();}}};/** * 用於更新ui */@SuppressLint("HandlerLeak")private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {final int x = msg.what;final int temp = (int) msg.obj;if (x == 0 * 1) {if (pro1 - temp > 0) {r1.setProgress(temp);r2.setProgress(temp);r3.setProgress(temp);r4.setProgress(temp);r5.setProgress(temp);} else {r1.setProgress(pro1);r2.setProgress(pro1);r3.setProgress(pro1);r4.setProgress(pro1);r5.setProgress(pro1);thread1.stopThread();}} else if (x == 0 * 2) {if (pro1 - temp > 0) {r1.setProgress(temp);r2.setProgress(temp);r3.setProgress(temp);r4.setProgress(temp);r5.setProgress(temp);} else {r1.setProgress(pro1);r2.setProgress(pro1);r3.setProgress(pro1);r4.setProgress(pro1);r5.setProgress(pro1);thread2.stopThread();}}};};/** * 線程,控制進度動畫 * @author zengtao 2015年6月10日 下午4:31:11 * */class MyThread extends Thread {@Overridepublic void run() {while (!flag) {try {progress += 1;Message msg = new Message();msg.what = 0 * 1;msg.obj = progress;Thread.sleep(100);mHandler.sendMessage(msg);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopThread() {flag = true;}}/** * 線程,控制進度動畫 * @author zengtao 2015年6月10日 下午4:31:11 * */class MyThread2 extends Thread {@Overridepublic void run() {while (!flag) {try {progress += 2;Message msg = new Message();msg.what = 0 * 2;msg.obj = progress;Thread.sleep(200);mHandler.sendMessage(msg);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopThread() {flag = true;}}@Overrideprotected void onDestroy() {super.onDestroy();if (thread1 != null) {thread1.stopThread();}if (thread2 != null) {thread2.stopThread();}}}
四.總結實現以上步驟,就實現了一個安全可靠的自訂控制項的實現,看起來簡簡單單,不說分分鐘,但是稍微花點時間這就是你的。
源碼:http://download.csdn.net/detail/u011546655/8793521