1. 程式人生 > >自定義ProgressBar實現圓弧進度條

自定義ProgressBar實現圓弧進度條

依賴

dependencies {
  compile 'com.czp.arcProgressBar:ArcProgressBar:1.0.1'
}
nameformatdescription
borderWidthinteger圓弧邊框的寬度
progressStyletick/arc進度條型別,tick為帶刻度的
radiusinteger半徑
arcbgColorcolor圓弧的邊框背景
degreeinteger弧度,設定為0即為圓形進度條,180為半圓
tickWidthinteger刻度的寬度
tickDensityinteger刻度的密度  2~8 越小越密
bgShowboolean是否顯示圓弧邊框背景
arcCapRoundboolean圓弧的筆觸是否為圓形,tick無效

interface

提供了繪製圓弧中間區域的一個介面 ,可根據自己的需求自由繪製

123456789101112/*** @param canvas  * @param rectF  圓弧的Rect* @param x      圓弧的中心x* @param y      圓弧的中心y* @param storkeWidth   圓弧的邊框寬度* @param progress      當前進度*/public interface  OnCenterDraw {public  void draw(Canvas canvas, RectF rectF, 
float x, float y,float storkeWidth,int progress);}

預設提供了兩個實現 onImageCenter and OnTextCenter

Use

123456789mProgress.setOnCenterDraw(new ArcProgress.OnCenterDraw() {            @Overridepublic void draw(Canvas canvas, RectF rectF, float x, float y, float storkeWidth,int progress) {Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setStrokeWidth(35);textPaint.setColor(getResources().getColor(R.color.textColor));String progressStr = String.valueOf(progress+"%");                float textX = x-(textPaint.measureText(progressStr)/2);                float textY = y-((textPaint.descent()+textPaint.ascent())/2);canvas.drawText(progressStr,textX,textY,textPaint);}});

依賴



https://github.com/ZeeeeeeNo/ArcProgressBar(原始碼地址);