1 Star 0 Fork 0

xiaow / AndroidPractiseCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ArrowLoadingView.kt 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
xiaowu 提交于 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 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()
}
}
Android
1
https://gitee.com/jxiaow/practiseCodeAndroid.git
git@gitee.com:jxiaow/practiseCodeAndroid.git
jxiaow
practiseCodeAndroid
AndroidPractiseCode
master

搜索帮助