Created
April 10, 2022 11:37
-
-
Save andreymusth/409920ae3aba0699f5f63353bf676f5a to your computer and use it in GitHub Desktop.
Calculating next frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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