Skip to content

Instantly share code, notes, and snippets.

@GregKluska
Last active November 21, 2021 21:43
Show Gist options
  • Save GregKluska/0931852cc64601abf5e84005798e26ea to your computer and use it in GitHub Desktop.
Save GregKluska/0931852cc64601abf5e84005798e26ea to your computer and use it in GitHub Desktop.
Animatable Offset and Shake Animation
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.ui.geometry.Offset
private val offsetToVector: TwoWayConverter<Offset, AnimationVector2D> =
TwoWayConverter({ AnimationVector2D(it.x, it.y) },
{ Offset(it.v1, it.v2) })
fun Animatable(initialValue: Offset): Animatable<Offset, AnimationVector2D> =
Animatable(initialValue, offsetToVector)
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.keyframes
import androidx.compose.ui.geometry.Offset
val shakeKeyframes: AnimationSpec<Offset> = keyframes {
durationMillis = 820
val multiplier = 2f
val easing = CubicBezierEasing(0.36f, 0.07f, 0.19f, 0.97f)
Offset(1F * multiplier, 1F * multiplier) at durationMillis / 10 * 1 with easing
Offset(-1F * multiplier, -2F * multiplier) at durationMillis / 10 * 2 with easing
Offset(-3F * multiplier, 0F * multiplier) at durationMillis / 10 * 3 with easing
Offset(3F * multiplier, 2F * multiplier) at durationMillis / 10 * 4 with easing
Offset(1F * multiplier, -1F * multiplier) at durationMillis / 10 * 5 with easing
Offset(-1F * multiplier, 2F * multiplier) at durationMillis / 10 * 6 with easing
Offset(-3F * multiplier, 1F * multiplier) at durationMillis / 10 * 7 with easing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment