Created
November 7, 2021 17:32
-
-
Save EmmanuelGuther/3489623f0e9c8f47bec144dfe4a50834 to your computer and use it in GitHub Desktop.
Background with items in movement in jetpack compose example
This file contains hidden or 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
@Preview | |
@Composable | |
fun BackgroundItemsMovement() { | |
var backgroundItemsMovementState by remember { mutableStateOf(PositionState.Start) } | |
Box(modifier = Modifier.fillMaxSize()) { | |
Row(modifier = Modifier.fillMaxSize()) { | |
repeat(4) { | |
BuildImg( | |
multiply = Random.nextInt(500, 4500), | |
state = backgroundItemsMovementState | |
) | |
} | |
} | |
Button( | |
onClick = { | |
backgroundItemsMovementState = when (backgroundItemsMovementState) { | |
PositionState.Start -> PositionState.Finish | |
PositionState.Finish -> PositionState.Start | |
} | |
}, modifier = Modifier | |
.fillMaxSize() | |
.wrapContentSize(align = Alignment.Center) | |
) { | |
Text(text = "Start") | |
} | |
} | |
} | |
@Composable | |
private fun offset(multiply: Int, state: PositionState): Dp = animateDpAsState( | |
targetValue = if (state == PositionState.Start) (800).dp else (-150).dp, | |
animationSpec = infiniteRepeatable(tween(4000 + multiply), RepeatMode.Restart) | |
).value | |
@Composable | |
private fun BuildImg(multiply: Int, state: PositionState) { | |
Image( | |
painter = painterResource(R.drawable.ic_cloud1), | |
contentDescription = null, | |
modifier = Modifier | |
.height(90.dp) | |
.absoluteOffset(y = offset(multiply = multiply, state)) | |
) | |
} | |
enum class PositionState { | |
Start, Finish | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment