Last active
May 30, 2021 08:39
-
-
Save zskamljic/20af686ee9b97a4dceba856f03e5e3b2 to your computer and use it in GitHub Desktop.
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
layout(constraints.maxWidth, size.height + contentHeight) { | |
val startOffset = size.height / 2 - placeables[0].height / 2 | |
var yPosition = startOffset | |
// Get the current ratio as float, otherwise it's either 0 or 1 | |
val scrollPercent = scrollState.value.toFloat() / scrollState.maxValue | |
// We need the element index now | |
placeables.forEachIndexed { index, placeable -> | |
// Get the ratio at which the item appears | |
val elementRatio = index.toFloat() / placeables.lastIndex | |
// Get the cosine of the difference, I chose to multiply by PI to get a shorter cosine period, lower values will make the curve longer | |
val interpolatedValue = cos((scrollPercent - elementRatio) * PI) | |
// Cosine function is limited to range [-1, 1], so we need to scale it to desired width, horizontal center in our case | |
val indent = interpolatedValue * size.width / 2 | |
placeable.placeRelative(x = indent.toInt(), y = yPosition) | |
yPosition += placeable.height + itemSpacing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment