1 Star 0 Fork 0

xiaow / AndroidPractiseCode

Gitee — Enterprise-level DevOps R&D management platform
Join Gitee
Clone or download
LoadingView.kt 1.86 KB
Copy Edit Web IDE Raw Blame History
xiaowu authored 2021-01-11 17:27 . add.
package com.github.jxiaow.sample.pathmeasure
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import kotlin.math.absoluteValue
class LoadingView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val paint = Paint().apply {
color = Color.BLUE
style = Paint.Style.STROKE
strokeWidth = 2.0f
isAntiAlias = true
}
private val path = Path()
private val pathMeasure = PathMeasure()
private val dest = Path()
private var favor = 0.5f
private val animator = ValueAnimator.ofFloat(0f, 1.0f)
.apply {
repeatCount = ValueAnimator.INFINITE
duration = 2000
interpolator = LinearInterpolator()
addUpdateListener {
favor = it.animatedValue as Float
invalidate()
}
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
path.addCircle(measuredWidth / 2f, measuredHeight / 2f, 200f, Path.Direction.CW)
pathMeasure.setPath(path, true)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
dest.reset()
dest.lineTo(0f, 0f)
val stopD = pathMeasure.length * favor
val startD = stopD - (0.5f - favor).absoluteValue * pathMeasure.length
pathMeasure.getSegment(startD, stopD, dest, true)
canvas.drawPath(dest, paint)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
animator.start()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
animator.cancel()
}
}

Comment ( 0 )

Sign in for post a comment

Android
1
https://gitee.com/jxiaow/practiseCodeAndroid.git
git@gitee.com:jxiaow/practiseCodeAndroid.git
jxiaow
practiseCodeAndroid
AndroidPractiseCode
master

Search

122604 9befe709 551147 122411 94cd1624 551147