@@ -6,25 +6,16 @@ import android.graphics.Color
6
6
import android.graphics.Paint
7
7
import android.graphics.Rect
8
8
import android.graphics.RectF
9
- import android.os.Handler
10
- import android.os.Message
11
9
import android.text.TextPaint
12
10
import android.util.AttributeSet
13
11
import android.view.View
14
12
import com.pengxh.kt.lite.R
15
13
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
22
14
23
15
/* *
24
16
* 圆形进度条
25
17
*/
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) {
28
19
29
20
private val ringRadius: Int
30
21
private var rectF: RectF
@@ -53,7 +44,6 @@ class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : Vie
53
44
54
45
// 当前测量值转为弧度扫过的角度
55
46
private var sweepAngle = 0f
56
- private val weakReferenceHandler by lazy { WeakReferenceHandler (this ) }
57
47
58
48
init {
59
49
val type = context.obtainStyledAttributes(attrs, R .styleable.CircleProgressBar )
@@ -216,36 +206,18 @@ class CircleProgressBar constructor(context: Context, attrs: AttributeSet) : Vie
216
206
else -> " $value %"
217
207
}
218
208
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 )
224
219
}
225
220
}
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
+ })
250
222
}
251
223
}
0 commit comments