1 Star 0 Fork 0

xiaow / AndroidPractiseCode

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
ArrowLoadingView.kt 2.46 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 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()
}
}

Comment ( 0 )

Sign in to post a comment

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

Search

161121 f78d6d6f 1850385 154831 86f8c370 1850385