自定義視圖view之環(huán)形進(jìn)度條
一、普通效果。
本章博客有4種不同的效果,小伙伴可以繪制更多的效果,唯一不同的代碼,是自定義view時,怎么繪制弧度。Main類和布局無更改”

package tester.ermu.com.pingamedemo;
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.util.Log;
import android.view.View;
/**
* 仿iphone帶進(jìn)度的進(jìn)度條,線程安全的View,可直接在線程中更新進(jìn)度
* @author xiaanming
*
*/
public class RoundProgressBar extends View {
/**
* 畫筆對象的引用
*/
private Paint paint;
/**
* 圓環(huán)的顏色
*/
private int roundColor;
/**
* 圓環(huán)進(jìn)度的顏色
*/
private int roundProgressColor;
/**
* 中間進(jìn)度百分比的字符串的顏色
*/
private int textColor;
/**
* 中間進(jìn)度百分比的字符串的字體
*/
private float textSize;
/**
* 圓環(huán)的寬度
*/
private float roundWidth;
/**
* 最大進(jìn)度
*/
private int max;
/**
* 當(dāng)前進(jìn)度
*/
private int progress;
/**
* 是否顯示中間的進(jìn)度
*/
private boolean textIsDisplayable;
/**
* 進(jìn)度的風(fēng)格,實心或者空心
*/
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);
//獲取自定義屬性和默認(rèn)值
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進(jìn)度的顏色
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進(jìn)度條最大值,一般都為1001
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進(jìn)度
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進(jìn)度的風(fēng)格,實心或者空心
mTypedArray.recycle();
}
/**
* 設(shè)置進(jìn)度,并判斷進(jìn)度狀態(tài)
* 刷新界面調(diào)用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();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫最外層的大圓環(huán)
*/
int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
Log.i("Text","centre ------1111"+centre);
int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑
Log.i("Text","radius ------2222"+radius);
paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setAntiAlias(true); //消除鋸齒
canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)
Log.e("log", centre + "");
/**
* 畫進(jìn)度百分比
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
int percent = (int)(((float)progress / (float)max) * 100); //中間的進(jìn)度百分比,先轉(zhuǎn)換成float在進(jìn)行除法運算,不然都為0
float textWidth = paint.measureText(percent + "%"); //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間
if(textIsDisplayable && percent != 0 && style == STROKE){
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進(jìn)度百分比
}
/**
* 畫圓弧 ,畫圓環(huán)的進(jìn)度
*/
//設(shè)置進(jìn)度是實心還是空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setColor(roundProgressColor); //設(shè)置進(jìn)度的顏色
RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
// RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);
// RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);
switch (style) {
/*
* public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
oval :指定圓弧的外輪廓矩形區(qū)域。
startAngle: 圓弧起始角度,單位為度。
sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
*
* */
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
// paint.setStrokeWidth(9);
// paint.setColor(Color.YELLOW);
// canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
if(progress !=0)
/*最外圍紅色圓弧*/
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
// paint.setStrokeWidth(9);
// paint.setColor(Color.YELLOW);
// canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //
break;
}
}
}
}
二、單環(huán)在圈外畫弧度

和普通效果代碼不同的地方如下,屏蔽167行,打開168行即可:


package tester.ermu.com.pingamedemo;
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.util.Log;
import android.view.View;
/**
* 仿iphone帶進(jìn)度的進(jìn)度條,線程安全的View,可直接在線程中更新進(jìn)度
* @author xiaanming
*
*/
public class RoundProgressBar extends View {
/**
* 畫筆對象的引用
*/
private Paint paint;
/**
* 圓環(huán)的顏色
*/
private int roundColor;
/**
* 圓環(huán)進(jìn)度的顏色
*/
private int roundProgressColor;
/**
* 中間進(jìn)度百分比的字符串的顏色
*/
private int textColor;
/**
* 中間進(jìn)度百分比的字符串的字體
*/
private float textSize;
/**
* 圓環(huán)的寬度
*/
private float roundWidth;
/**
* 最大進(jìn)度
*/
private int max;
/**
* 當(dāng)前進(jìn)度
*/
private int progress;
/**
* 是否顯示中間的進(jìn)度
*/
private boolean textIsDisplayable;
/**
* 進(jìn)度的風(fēng)格,實心或者空心
*/
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);
//獲取自定義屬性和默認(rèn)值
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進(jìn)度的顏色
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進(jìn)度條最大值,一般都為1001
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進(jìn)度
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進(jìn)度的風(fēng)格,實心或者空心
mTypedArray.recycle();
}
/**
* 設(shè)置進(jìn)度,并判斷進(jìn)度狀態(tài)
* 刷新界面調(diào)用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();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫最外層的大圓環(huán)
*/
int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
Log.i("Text","centre ------1111"+centre);
int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑
Log.i("Text","radius ------2222"+radius);
paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setAntiAlias(true); //消除鋸齒
canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)
Log.e("log", centre + "");
/**
* 畫進(jìn)度百分比
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
int percent = (int)(((float)progress / (float)max) * 100); //中間的進(jìn)度百分比,先轉(zhuǎn)換成float在進(jìn)行除法運算,不然都為0
float textWidth = paint.measureText(percent + "%"); //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間
if(textIsDisplayable && percent != 0 && style == STROKE){
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進(jìn)度百分比
}
/**
* 畫圓弧 ,畫圓環(huán)的進(jìn)度
*/
//設(shè)置進(jìn)度是實心還是空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setColor(roundProgressColor); //設(shè)置進(jìn)度的顏色
// RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);
// RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);
switch (style) {
/*
* public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
oval :指定圓弧的外輪廓矩形區(qū)域。
startAngle: 圓弧起始角度,單位為度。
sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
*
* */
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
// paint.setStrokeWidth(9);
// paint.setColor(Color.YELLOW);
// canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
if(progress !=0)
/*最外圍紅色圓弧*/
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
// paint.setStrokeWidth(9);
// paint.setColor(Color.YELLOW);
// canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //
break;
}
}
}
}
三、雙環(huán)效果

代碼不同的地方


package tester.ermu.com.pingamedemo;
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.util.Log;
import android.view.View;
/**
* 仿iphone帶進(jìn)度的進(jìn)度條,線程安全的View,可直接在線程中更新進(jìn)度
* @author xiaanming
*
*/
public class RoundProgressBar extends View {
/**
* 畫筆對象的引用
*/
private Paint paint;
/**
* 圓環(huán)的顏色
*/
private int roundColor;
/**
* 圓環(huán)進(jìn)度的顏色
*/
private int roundProgressColor;
/**
* 中間進(jìn)度百分比的字符串的顏色
*/
private int textColor;
/**
* 中間進(jìn)度百分比的字符串的字體
*/
private float textSize;
/**
* 圓環(huán)的寬度
*/
private float roundWidth;
/**
* 最大進(jìn)度
*/
private int max;
/**
* 當(dāng)前進(jìn)度
*/
private int progress;
/**
* 是否顯示中間的進(jìn)度
*/
private boolean textIsDisplayable;
/**
* 進(jìn)度的風(fēng)格,實心或者空心
*/
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);
//獲取自定義屬性和默認(rèn)值
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進(jìn)度的顏色
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進(jìn)度條最大值,一般都為1001
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進(jìn)度
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進(jìn)度的風(fēng)格,實心或者空心
mTypedArray.recycle();
}
/**
* 設(shè)置進(jìn)度,并判斷進(jìn)度狀態(tài)
* 刷新界面調(diào)用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();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫最外層的大圓環(huán)
*/
int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
Log.i("Text","centre ------1111"+centre);
int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑
Log.i("Text","radius ------2222"+radius);
paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setAntiAlias(true); //消除鋸齒
canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)
Log.e("log", centre + "");
/**
* 畫進(jìn)度百分比
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
int percent = (int)(((float)progress / (float)max) * 100); //中間的進(jìn)度百分比,先轉(zhuǎn)換成float在進(jìn)行除法運算,不然都為0
float textWidth = paint.measureText(percent + "%"); //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間
if(textIsDisplayable && percent != 0 && style == STROKE){
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進(jìn)度百分比
}
/**
* 畫圓弧 ,畫圓環(huán)的進(jìn)度
*/
//設(shè)置進(jìn)度是實心還是空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setColor(roundProgressColor); //設(shè)置進(jìn)度的顏色
// RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40); //--2號:用于定義的圓弧的形狀和大小的界限
RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);
// RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);
switch (style) {
/*
* public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
oval :指定圓弧的外輪廓矩形區(qū)域。
startAngle: 圓弧起始角度,單位為度。
sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
*
* */
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
paint.setStrokeWidth(9);
paint.setColor(Color.YELLOW);
canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
if(progress !=0)
/*最外圍紅色圓弧*/
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
paint.setStrokeWidth(9);
paint.setColor(Color.YELLOW);
canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //
break;
}
}
}
}
實心效果環(huán)形進(jìn)度條

這里需要添加或修改的代碼如下:

package tester.ermu.com.pingamedemo;
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.util.Log;
import android.view.View;
/**
* 仿iphone帶進(jìn)度的進(jìn)度條,線程安全的View,可直接在線程中更新進(jìn)度
* @author xiaanming
*
*/
public class RoundProgressBar extends View {
/**
* 畫筆對象的引用
*/
private Paint paint;
/**
* 圓環(huán)的顏色
*/
private int roundColor;
/**
* 圓環(huán)進(jìn)度的顏色
*/
private int roundProgressColor;
/**
* 中間進(jìn)度百分比的字符串的顏色
*/
private int textColor;
/**
* 中間進(jìn)度百分比的字符串的字體
*/
private float textSize;
/**
* 圓環(huán)的寬度
*/
private float roundWidth;
/**
* 最大進(jìn)度
*/
private int max;
/**
* 當(dāng)前進(jìn)度
*/
private int progress;
/**
* 是否顯示中間的進(jìn)度
*/
private boolean textIsDisplayable;
/**
* 進(jìn)度的風(fēng)格,實心或者空心
*/
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);
//獲取自定義屬性和默認(rèn)值
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//圓環(huán)的顏色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);//圓環(huán)進(jìn)度的顏色
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);//字體顏色
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//字體大小
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);//圓環(huán)的寬度
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//進(jìn)度條最大值,一般都為1001
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//是否顯示中間的進(jìn)度
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 1);//進(jìn)度的風(fēng)格,實心或者空心
mTypedArray.recycle();
}
/**
* 設(shè)置進(jìn)度,并判斷進(jìn)度狀態(tài)
* 刷新界面調(diào)用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();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫最外層的大圓環(huán)
*/
int centre = getWidth()/2; //獲取圓心的x坐標(biāo)
Log.i("Text","centre ------1111"+centre);
int radius = (int) (centre - roundWidth*2); //圓環(huán)的半徑
Log.i("Text","radius ------2222"+radius);
paint.setColor(roundColor); //設(shè)置圓環(huán)的顏色
paint.setStyle(Paint.Style.STROKE); //設(shè)置空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setAntiAlias(true); //消除鋸齒
canvas.drawCircle(centre, centre, radius, paint); //畫出圓環(huán)
Log.e("log", centre + "");
/**
* 畫進(jìn)度百分比
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體
int percent = (int)(((float)progress / (float)max) * 100); //中間的進(jìn)度百分比,先轉(zhuǎn)換成float在進(jìn)行除法運算,不然都為0
float textWidth = paint.measureText(percent + "%"); //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間
if(textIsDisplayable && percent != 0 && style == STROKE){
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進(jìn)度百分比
}
/**
* 畫圓弧 ,畫圓環(huán)的進(jìn)度
*/
//設(shè)置進(jìn)度是實心還是空心
paint.setStrokeWidth(roundWidth); //設(shè)置圓環(huán)的寬度
paint.setColor(roundProgressColor); //設(shè)置進(jìn)度的顏色
// RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius);//----1號:代碼用于定義的圓弧的形狀和大小的界限
// RectF oval = new RectF(centre - radius-40, centre - radius-40, centre+ radius+40, centre + radius+40); //--2號:用于定義的圓弧的形狀和大小的界限
// RectF ova12 = new RectF(centre - radius-60, centre - radius-60, centre+ radius+60, centre + radius+60);
RectF oval = new RectF(centre - radius+40, centre - radius+40, centre+ radius-40, centre + radius-40); //--2號:用于定義的圓弧的形狀和大小的界限
RectF ova12 = new RectF(centre - radius+60, centre - radius+60, centre+ radius-60, centre + radius-60);
switch (style) {
/*
* public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
oval :指定圓弧的外輪廓矩形區(qū)域。
startAngle: 圓弧起始角度,單位為度。
sweepAngle: 圓弧掃過的角度,順時針方向,單位為度。
useCenter: 如果為True時,在繪制圓弧時將圓心包括在內(nèi),通常用來繪制扇形。
paint: 繪制圓弧的畫板屬性,如顏色,是否填充等。
*
* */
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
paint.setStrokeWidth(9);
paint.setColor(Color.YELLOW);
canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //根據(jù)進(jìn)度畫圓弧
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(18);
paint.setColor(Color.RED);
if(progress !=0)
/*最外圍紅色圓弧*/
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據(jù)進(jìn)度畫圓弧
/*最外圍黃色圓弧*/
paint.setStrokeWidth(9);
paint.setColor(Color.YELLOW);
canvas.drawArc(ova12, 0, 360 * progress / max, false, paint); //
break;
}
}
}
}
四、Xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android_custom="http://schemas.android.com/apk/res/tester.ermu.com.pingamedemo"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
tools:ignore="ResAuto">
<tester.ermu.com.pingamedemo.RoundProgressBar
android:id="@+id/roundBar"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="79dp"
android_custom:roundColor="#ffffff"
android_custom:roundProgressColor="@android:color/black"
android_custom:textColor="#9A32CD"
android_custom:roundWidth="15dip"
android_custom:textSize="18sp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:layout_margin="5dp"
android:text="開始" />
</LinearLayout>
五、MainActivity中代碼的引用
package tester.ermu.com.pingamedemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private RoundProgressBar roundBar ;
private int progress = 0;
private Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cricle_progress);
init();
}
private void init() {
roundBar = (RoundProgressBar) findViewById(R.id.roundBar);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
new Thread(new Runnable() {
@Override
public void run() {
while(progress <= 100){
progress += 3;
System.out.println(progress);
roundBar.setProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
break;
}
}
}
六、自定義屬性
<?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>
到此這篇關(guān)于自定義視圖view之環(huán)形進(jìn)度條的文章就介紹到這了,更多相關(guān)自定義view環(huán)形進(jìn)度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入了解Android中GestureDetector的定義與使用
Android中的GestureDetector?可以使用?MotionEvents?檢測各種手勢和事件,非常的好用。本文將會通過幾個具體的例子來講解一下GestureDetector的具體使用方法,需要的可以參考一下2023-01-01
android項目實現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息
本篇文章主要介紹了android項目實現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息,就是實現(xiàn)在通知欄看到下載進(jìn)度。具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10
Android實現(xiàn)數(shù)字跳動效果的TextView方法示例
數(shù)字跳動效果相信大家應(yīng)該都見過,在開發(fā)加上這種效果后會讓ui交互看起來非常不錯,所以下面這篇文章主要給大家介紹了Android實現(xiàn)數(shù)字跳動的TextView的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04
Android WebView實現(xiàn)頂部進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android WebView實現(xiàn)頂部進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Android監(jiān)聽輸入法彈窗和關(guān)閉的實現(xiàn)方法
用過ios的都知道ios上輸入法關(guān)閉的同時會自動關(guān)閉輸入框,那么在android上如何實現(xiàn)監(jiān)聽輸入法彈出和關(guān)閉呢?接下來通過本文給大家分享一種可靠的實現(xiàn)方式2016-11-11
Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

