优化圆形progressbar+1
This commit is contained in:
parent
c4855e93cf
commit
58e3bde0c4
|
|
@ -49,7 +49,6 @@ class CircleProgressBar @JvmOverloads constructor(
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取自定义属性
|
|
||||||
progressColor = typedArray.getColor(
|
progressColor = typedArray.getColor(
|
||||||
R.styleable.CircleProgressBar_progressColor,
|
R.styleable.CircleProgressBar_progressColor,
|
||||||
ContextCompat.getColor(context, R.color.progress_green)
|
ContextCompat.getColor(context, R.color.progress_green)
|
||||||
|
|
@ -62,7 +61,7 @@ class CircleProgressBar @JvmOverloads constructor(
|
||||||
|
|
||||||
progressWidth = typedArray.getDimension(
|
progressWidth = typedArray.getDimension(
|
||||||
R.styleable.CircleProgressBar_progressWidth,
|
R.styleable.CircleProgressBar_progressWidth,
|
||||||
dpToPx(2f)
|
2.dp.toFloat()
|
||||||
)
|
)
|
||||||
|
|
||||||
val iconRes = typedArray.getResourceId(R.styleable.CircleProgressBar_centerIcon, 0)
|
val iconRes = typedArray.getResourceId(R.styleable.CircleProgressBar_centerIcon, 0)
|
||||||
|
|
@ -88,7 +87,6 @@ class CircleProgressBar @JvmOverloads constructor(
|
||||||
progressPaint.color = progressColor
|
progressPaint.color = progressColor
|
||||||
progressPaint.strokeWidth = progressWidth
|
progressPaint.strokeWidth = progressWidth
|
||||||
|
|
||||||
// 设置半透明效果,模仿图片中的绿色弧线
|
|
||||||
progressPaint.alpha = 180
|
progressPaint.alpha = 180
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,16 +112,16 @@ class CircleProgressBar @JvmOverloads constructor(
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
|
|
||||||
// 绘制背景圆环
|
// 背景
|
||||||
canvas.drawArc(rectF, 0f, 360f, false, backgroundPaint)
|
canvas.drawArc(rectF, 0f, 360f, false, backgroundPaint)
|
||||||
|
|
||||||
// 绘制进度弧线
|
// 进度弧线
|
||||||
if (currentProgress > 0) {
|
if (currentProgress > 0) {
|
||||||
val sweepAngle = (currentProgress.toFloat() / maxProgress) * 360f
|
val sweepAngle = (currentProgress.toFloat() / maxProgress) * 360f
|
||||||
canvas.drawArc(rectF, startAngle, sweepAngle, false, progressPaint)
|
canvas.drawArc(rectF, startAngle, sweepAngle, false, progressPaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制中心图标
|
// 中心图标
|
||||||
centerIcon?.let { icon ->
|
centerIcon?.let { icon ->
|
||||||
drawCenterIcon(canvas, icon)
|
drawCenterIcon(canvas, icon)
|
||||||
}
|
}
|
||||||
|
|
@ -186,9 +184,6 @@ class CircleProgressBar @JvmOverloads constructor(
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dpToPx(dp: Float): Float {
|
|
||||||
return dp * resources.displayMetrics.density
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDetachedFromWindow() {
|
override fun onDetachedFromWindow() {
|
||||||
super.onDetachedFromWindow()
|
super.onDetachedFromWindow()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue