This project demonstrates how to create a dynamic button shaking animation using Jetpack Compose. The button shakes in different directions and strengths to enhance user interaction.
- Shake in various directions:
LEFT
,RIGHT
,LEFT_THEN_RIGHT
,RIGHT_THEN_LEFT
- Customize the strength of the shake:
Normal
,Strong
,Custom
Check out the video below to see the shaking button in action:
shaking_black.webm
Follow these instructions to get a copy of the project up and running on your local machine.
- Clone the repository:
git clone https://github.com/kemallakkus/ShakingJetpackCompose
- Open the project in Android Studio.
- Build and run the project on an emulator or a physical device.
You can customize the shaking settings by modifying the rememberShakingState
function in the MainActivity
:
val shakingState = rememberShakingState(
strength = ShakingState.Strength.Custom(100f),
direction = ShakingState.Directions.LEFT_THEN_RIGHT
)
ShakingState.Strength.Normal:
Default strength (17f)ShakingState.Strength.Strong:
Stronger shake (40f)ShakingState.Strength.Custom(value):
Custom strength, where value is a float.
ShakingState.Directions.LEFT:
Shake to the leftShakingState.Directions.RIGHT:
Shake to the rightShakingState.Directions.LEFT_THEN_RIGHT:
Shake left then rightShakingState.Directions.RIGHT_THEN_LEFT:
Shake right then left
Here is an example of how to apply the shaking effect to a button:
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
val shakingState = rememberShakingState(
strength = ShakingState.Strength.Custom(100f),
direction = ShakingState.Directions.LEFT_THEN_RIGHT
)
val scope = rememberCoroutineScope()
TextButton(
onClick = {
scope.launch {
shakingState.shake(animationDuration = 30)
}
},
modifier = Modifier.shakable(shakingState)
) {
Text(text = "Shakiiing !!", fontSize = 22.sp)
}
}