Skip to content

Instantly share code, notes, and snippets.

@andrewbel31
Last active April 10, 2022 11:30
Show Gist options
  • Save andrewbel31/13a29844673452a7e3b8acadb06ce382 to your computer and use it in GitHub Desktop.
Save andrewbel31/13a29844673452a7e3b8acadb06ce382 to your computer and use it in GitHub Desktop.
Updating amplitude
class DynamicView
@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var animateToAmplitude = 0f
private var amplitude = 0f
private var deltaAmplitude = 0f
var speed = Speed.HIGH
init {
setWillNotDraw(false)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// draw something depending on current amplitude value
invalidate()
}
fun setAmplitude(value: Float) {
animateToAmplitude = value
val diff = animateToAmplitude - amplitude // current amplitude stored in view
if (animateToAmplitude > amplitude) {
deltaAmplitude = diff / (100f + 600f * speed.coef)
} else {
deltaAmplitude = diff / (100f + 1000f * speed.coef)
}
}
enum class Speed(val coef: Float) {
HIGH(0.35f),
SLOW(0.50f)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment