Android控制元件之圓形進度條
阿新 • • 發佈:2018-11-12
Android-自定義ProgressBar實現圓弧進度條
在之前的專案中用到過這個,感覺還是非常實用的,我實現的是額度的增長.
繼承於ProgressBar實現,保留了Progressbar的特性,原始碼在文尾。
引數
name | format | description |
---|---|---|
borderWidth | integer | 圓弧邊框的寬度 |
progressStyle | tick/arc | 進度條型別,tick為帶刻度的 |
radius | integer | 半徑 |
arcbgColor | color | 圓弧的邊框背景 |
degree | integer | 弧度,設定為0即為圓形進度條,180為半圓 |
tickWidth | integer | 刻度的寬度 |
tickDensity | integer | 刻度的密度 2~8 越小越密 |
bgShow | boolean | 是否顯示圓弧邊框背景 |
arcCapRound | boolean | 圓弧的筆觸是否為圓形,tick無效 |
interface
提供了繪製圓弧中間區域的一個介面 ,可根據自己的需求自由繪製
1 2 3 4 5 6 7 8 9 10 11 12 |
/**
*
* @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
1 2 3 4 5 6 7 8 9 |
mProgress.setOnCenterDraw(
new
ArcProgress.OnCenterDraw() { @Override
public
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);
}
});
|
依賴
dependencies { compile 'com.czp.arcProgressBar:ArcProgressBar:1.0.1' }