Created
September 27, 2023 08:21
-
-
Save kaszabimre/b62e6bcaa39534cf9d73918bbc65afde to your computer and use it in GitHub Desktop.
ParallaxCarouselItem where the magic happens
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
@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