自定義視圖view之環(huán)形進(jìn)度條
一、普通效果。
本章博客有4種不同的效果,小伙伴可以繪制更多的效果,唯一不同的代碼,是自定義view時(shí),怎么繪制弧度。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)格,實(shí)心或者空心 */ 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)格,實(shí)心或者空心 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)行除法運(yù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)度是實(shí)心還是空心 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: 圓弧掃過的角度,順時(shí)針方向,單位為度。 useCenter: 如果為True時(shí),在繪制圓弧時(shí)將圓心包括在內(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)格,實(shí)心或者空心 */ 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)格,實(shí)心或者空心 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)行除法運(yù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)度是實(shí)心還是空心 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: 圓弧掃過的角度,順時(shí)針方向,單位為度。 useCenter: 如果為True時(shí),在繪制圓弧時(shí)將圓心包括在內(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)格,實(shí)心或者空心 */ 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)格,實(shí)心或者空心 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)行除法運(yù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)度是實(shí)心還是空心 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: 圓弧掃過的角度,順時(shí)針方向,單位為度。 useCenter: 如果為True時(shí),在繪制圓弧時(shí)將圓心包括在內(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; } } } }
實(shí)心效果環(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)格,實(shí)心或者空心 */ 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)格,實(shí)心或者空心 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)行除法運(yù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)度是實(shí)心還是空心 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: 圓弧掃過的角度,順時(shí)針方向,單位為度。 useCenter: 如果為True時(shí),在繪制圓弧時(shí)將圓心包括在內(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 模擬器(JAVA)與C++ socket 通訊 分享
Android 模擬器(JAVA)與C++ socket 通訊 分享,需要的朋友可以參考一下2013-05-05Activity跳轉(zhuǎn)時(shí)生命周期跟蹤的實(shí)例
下面小編就為大家?guī)硪黄狝ctivity跳轉(zhuǎn)時(shí)生命周期跟蹤的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法
這篇文章主要為大家提供了一個(gè)解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法,感興趣的小伙伴們可以參考一下2016-02-02Android 實(shí)現(xiàn)IOS選擇拍照相冊底部彈出的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)IOS選擇拍照相冊底部彈出的實(shí)例的相關(guān)資料,這里提供了實(shí)現(xiàn)效果圖及實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-07-07Android進(jìn)階教程之ViewGroup自定義布局
這篇文章主要給大家介紹了關(guān)于Android進(jìn)階教程之ViewGroup自定義布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Android 中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí)
本文給大家分享android中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01Android實(shí)現(xiàn)TCP客戶端支持讀寫操作
這篇文章主要介紹了Android-實(shí)現(xiàn)TCP客戶端,支持讀寫操作,主要是通過socket讀寫tcp,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Android點(diǎn)擊事件的實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件的實(shí)現(xiàn)方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android組件Activity的啟動(dòng)過程深入分析
這篇文章主要介紹了Android組件Activity的啟動(dòng)過程,Activity作為Android四大組件之一,他的啟動(dòng)沒有那么簡單。這里涉及到了系統(tǒng)服務(wù)進(jìn)程,啟動(dòng)過程細(xì)節(jié)很多,這里我只展示主體流程。activity的啟動(dòng)流程隨著版本的更替,代碼細(xì)節(jié)一直在進(jìn)行更改,每次都會(huì)有很大的修改2023-04-04