Skip to content

Instantly share code, notes, and snippets.

@zskamljic
Last active May 30, 2021 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zskamljic/20af686ee9b97a4dceba856f03e5e3b2 to your computer and use it in GitHub Desktop.
Save zskamljic/20af686ee9b97a4dceba856f03e5e3b2 to your computer and use it in GitHub Desktop.
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