Skip to content

Instantly share code, notes, and snippets.

@kaszabimre
Created September 27, 2023 08:21
Show Gist options
  • Save kaszabimre/b62e6bcaa39534cf9d73918bbc65afde to your computer and use it in GitHub Desktop.
Save kaszabimre/b62e6bcaa39534cf9d73918bbc65afde to your computer and use it in GitHub Desktop.
ParallaxCarouselItem where the magic happens
@Composable
fun ParallaxCarouselItem(
imageResId: Int,
parallaxOffset: Float,
pagerHeight: Dp,
screenWidth: Dp,
density: Float,
) {
// Load the image bitmap
val imageBitmap = ImageBitmap.imageResource(id = imageResId)
// Calculate the draw size for the image
val drawSize = imageBitmap.calculateDrawSize(density, screenWidth, pagerHeight)
// Card composable for the item
Card(
modifier = Modifier
.fillMaxSize()
.padding(cardPadding)
.background(Color.White, shape)
.shadow(elevation = shadowElevation, shape = shape)
) {
// Canvas for drawing the image with parallax effect
Canvas(
modifier = Modifier
.fillMaxSize()
.padding(imagePadding)
.clip(shape)
) {
// Translate the canvas for parallax effect
translate(left = parallaxOffset * density) {
// Draw the image
drawImage(
image = imageBitmap,
srcSize = IntSize(imageBitmap.width, imageBitmap.height),
dstOffset = IntOffset(-xOffset.toIntPx(density), 0),
dstSize = drawSize,
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment