Skip to content

Instantly share code, notes, and snippets.

@ken-itakura
Created February 3, 2023 07:28
Show Gist options
  • Save ken-itakura/db99778f059e792de27695037debfc35 to your computer and use it in GitHub Desktop.
Save ken-itakura/db99778f059e792de27695037debfc35 to your computer and use it in GitHub Desktop.
Android simple shrinking circle animation
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.view.View
import android.view.animation.Animation
/* How to use
Call as follows in an Activity
val circleView = CircleView(this)
setContentView(circleView)
val circleAnimation = ShrinkCircleAnimation(circleView, 10, 300)
circleAnimation.setDuration(5000)
circleView.startAnimation(circleAnimation)
*/
class CircleView(context: Context?) : View(context) {
var radius = 0f
override fun onDraw(canvas: Canvas) {
val centerX = (canvas.width /2).toFloat()
val centerY = (canvas.height /2).toFloat()
val paint: Paint = Paint().apply { setARGB(255, 0, 255, 255) }
.apply{setStyle(Paint.Style.STROKE)}.apply { setStrokeWidth(2.0F) }
canvas.drawCircle(centerX, centerY, radius, paint)
}
}
class ShrinkCircleAnimation internal constructor(val circleView: CircleView, private val start: Int, private val end: Int) : Animation() {
protected override fun applyTransformation(
interpolatedTime: Float, transformation: android.view.animation.Transformation
) {
// interpolatedTime: 0.f -> 1.0f
val radius = (end -((end - start) * interpolatedTime))
circleView.radius = radius
circleView.requestLayout()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment