Skip to content

Instantly share code, notes, and snippets.

@andreymusth
Created April 10, 2022 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreymusth/409920ae3aba0699f5f63353bf676f5a to your computer and use it in GitHub Desktop.
Save andreymusth/409920ae3aba0699f5f63353bf676f5a to your computer and use it in GitHub Desktop.
Calculating next frame
class DynamicView
@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var lastUpdateTime: Long = System.currentTimeMillis()
// ..
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val delta = System.currentTimeMillis() - lastUpdateTime
calculateNextFrame(delta)
val radius = minRadius + (maxRadius - minRadius) * amplitude / MAX_AMPLITUDE
canvas.drawCircle(width / 2f, height / 2f, radius, paint)
lastUpdateTime = System.currentTimeMillis()
invalidate()
}
private fun calculateNextFrame(dt: Long) {
if (animateToAmplitude != amplitude) {
amplitude += deltaAmplitude * dt
if (deltaAmplitude > 0) {
amplitude = amplitude.coerceAtMost(animateToAmplitude)
} else {
amplitude = amplitude.coerceAtLeast(animateToAmplitude)
}
}
}
fun setAmplitude(value: Float) {
//..
}
private companion object {
private const val MAX_AMPLITUDE = 1200f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment