Skip to content

Commit 59608e0

Browse files
refactor(*): 优化CircleProgressBar
1 parent 4e8717e commit 59608e0

File tree

1 file changed

+12
-40
lines changed

1 file changed

+12
-40
lines changed

lite/src/main/java/com/pengxh/kt/lite/widget/CircleProgressBar.kt

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@ import android.graphics.Color
66
import android.graphics.Paint
77
import android.graphics.Rect
88
import android.graphics.RectF
9-
import android.os.Handler
10-
import android.os.Message
119
import android.text.TextPaint
1210
import android.util.AttributeSet
1311
import android.view.View
1412
import com.pengxh.kt.lite.R
1513
import com.pengxh.kt.lite.extensions.dp2px
16-
import com.pengxh.kt.lite.utils.WeakReferenceHandler
17-
import kotlinx.coroutines.CoroutineScope
18-
import kotlinx.coroutines.Dispatchers
19-
import kotlinx.coroutines.delay
20-
import kotlinx.coroutines.launch
21-
import kotlinx.coroutines.withContext
2214

2315
/**
2416
* 圆形进度条
2517
*/
26-
class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : View(context, attrs),
27-
Handler.Callback {
18+
class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : View(context, attrs) {
2819

2920
private val ringRadius: Int
3021
private var rectF: RectF
@@ -53,7 +44,6 @@ class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : Vie
5344

5445
//当前测量值转为弧度扫过的角度
5546
private var sweepAngle = 0f
56-
private val weakReferenceHandler by lazy { WeakReferenceHandler(this) }
5747

5848
init {
5949
val type = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar)
@@ -216,36 +206,18 @@ class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : Vie
216206
else -> "$value%"
217207
}
218208

219-
CoroutineScope(Dispatchers.Main).launch {
220-
withContext(Dispatchers.IO) {
221-
for (i in 0 until value) {
222-
weakReferenceHandler.post(updateProgressRunnable.setProgress(i))
223-
delay(10)
209+
val i = intArrayOf(0)
210+
post(object : Runnable {
211+
override fun run() {
212+
i[0]++
213+
sweepAngle = i[0].toFloat() * 360 / 100
214+
invalidate()
215+
if (i[0] <= value) {
216+
postDelayed(this, 10)
217+
} else {
218+
removeCallbacks(this)
224219
}
225220
}
226-
}
227-
}
228-
229-
private interface UpdateProgressRunnable : Runnable {
230-
fun setProgress(progress: Int): UpdateProgressRunnable
231-
}
232-
233-
private val updateProgressRunnable = object : UpdateProgressRunnable {
234-
235-
private var progress = 0
236-
237-
override fun setProgress(progress: Int): UpdateProgressRunnable {
238-
this.progress = progress
239-
return this
240-
}
241-
242-
override fun run() {
243-
sweepAngle = progress.toFloat() * 360 / 100
244-
invalidate()
245-
}
246-
}
247-
248-
override fun handleMessage(msg: Message): Boolean {
249-
return true
221+
})
250222
}
251223
}

0 commit comments

Comments
 (0)