代码拉取完成,页面将自动刷新
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 com.github.jxiaow.sample.R
import kotlin.math.PI
import kotlin.math.atan2
class ArrowLoadingView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
// 圆画笔
private val circlePaint = Paint().apply {
color = Color.BLUE
style = Paint.Style.STROKE
strokeWidth = 5.0f
isAntiAlias = true
}
private val arrowPaint = Paint().apply {
color = Color.GRAY
strokeWidth = 1f
isAntiAlias = true
isDither = true
style = Paint.Style.STROKE
}
private val bitmap = run {
val options = BitmapFactory.Options()
options.inSampleSize = 2
BitmapFactory.decodeResource(resources, R.drawable.arrow, options)
}
private val path = Path().apply {
addCircle(0f, 0f, 300f, Path.Direction.CW)
}
private val pathMeasure = PathMeasure(path, true)
private val arrowMatrix = Matrix()
private var favor = 0.5f
private val pos = FloatArray(2)
private val tans = FloatArray(2)
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.translate(width / 2f, height / 2f)
canvas.drawPath(path, circlePaint)
arrowMatrix.reset()
val distance = pathMeasure.length * favor
pathMeasure.getPosTan(distance, pos, tans)
val degress = atan2(tans[1], tans[0]) * 180 / PI
arrowMatrix.postRotate(degress.toFloat(), bitmap.width / 2f, bitmap.height / 2f)
arrowMatrix.postTranslate(pos[0] - bitmap.width / 2, pos[1] - bitmap.height / 2)
canvas.drawBitmap(bitmap, arrowMatrix, arrowPaint)
}
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 onAttachedToWindow() {
super.onAttachedToWindow()
animator.start()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
animator.cancel()
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。