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