Created
July 14, 2024 06:56
-
-
Save ardakazanci/6bc924a9bca434c83e5a959f41848c33 to your computer and use it in GitHub Desktop.
Jetpack Compose in ShaderBrush
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
@Composable | |
fun Modifier.starBackgroundAnimated(): Modifier { | |
val image = ImageBitmap.imageResource(R.drawable.bg_image) | |
val transition = rememberInfiniteTransition(label = "") | |
val offset by transition.animateFloat( | |
initialValue = 0f, | |
targetValue = image.width.toFloat(), | |
animationSpec = infiniteRepeatable( | |
animation = tween(durationMillis = 10000, easing = LinearEasing), | |
repeatMode = RepeatMode.Reverse | |
), label = "offset" | |
) | |
return this.then(Modifier.drawWithCache { | |
onDrawWithContent { | |
val shader = ImageShader(image, TileMode.Mirror, TileMode.Mirror) | |
val paint = Paint().apply { | |
this.shader = shader | |
} | |
withTransform({ | |
translate(left = -offset) | |
}) { | |
drawRect( | |
brush = ShaderBrush(shader), | |
size = Size(size.width + offset, size.height) | |
) | |
} | |
drawContent() | |
} | |
}) | |
} | |
@Composable | |
fun Modifier.animatedGradientBackground(): Modifier { | |
val transition = rememberInfiniteTransition() | |
val color1 by transition.animateColor( | |
initialValue = Color.White, | |
targetValue = Color.Red, | |
animationSpec = infiniteRepeatable( | |
animation = tween(durationMillis = 5000, easing = LinearEasing), | |
repeatMode = RepeatMode.Reverse | |
), label = "" | |
) | |
val color2 by transition.animateColor( | |
initialValue = Color.White.copy(alpha = 0.6f), | |
targetValue = Color.LightGray.copy(alpha = 0.6f), | |
animationSpec = infiniteRepeatable( | |
animation = tween(durationMillis = 5000, easing = LinearEasing), | |
repeatMode = RepeatMode.Reverse | |
), label = "" | |
) | |
val color3 by transition.animateColor( | |
initialValue = Color.White.copy(alpha = 0.4f), | |
targetValue = Color.Red.copy(alpha = 0.4f), | |
animationSpec = infiniteRepeatable( | |
animation = tween(durationMillis = 5000, easing = LinearEasing), | |
repeatMode = RepeatMode.Reverse | |
), label = "" | |
) | |
val color4 by transition.animateColor( | |
initialValue = Color.Transparent, | |
targetValue = Color.Magenta.copy(alpha = 0.4f), | |
animationSpec = infiniteRepeatable( | |
animation = tween(durationMillis = 5000, easing = LinearEasing), | |
repeatMode = RepeatMode.Reverse | |
), label = "" | |
) | |
return this.background( | |
Brush.verticalGradient( | |
0f to color1, | |
0.5f to color2, | |
0.85f to color3, | |
1f to color4 | |
) | |
) | |
} | |
@Preview | |
@Composable | |
fun PreviewStarBackgroundAnimated() { | |
Box( | |
modifier = Modifier | |
.size(1000.dp) | |
.background(Color.White) | |
.starBackgroundAnimated() | |
.animatedGradientBackground() | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment