Skip to content

Instantly share code, notes, and snippets.

@andreymusth
Last active April 10, 2022 11:30
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/13a29844673452a7e3b8acadb06ce382 to your computer and use it in GitHub Desktop.
Save andreymusth/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