Search code examples
androidimagekotlingradientandroid-jetpack-compose

How to apply translucent gradient on an Image in Android Jetpack Compose?


In Android Jetpack Compose, does anyone knows how to make an Image's left side slowly fading to transparent towards right side? Thanks!

Edit: Sorry, I mean making an image fading like this in Compose, probably with blend mode? But not sure how to do that..

Expected outcome:

Image fading to transparent sample


Solution

  • Just found an answer from How to add fading edge effect to Android Jetpack Compose Column or Row?

    "Color.Black" means area that reveal the image.

    Thanks guys!

    Image(
        painterResource(R.drawable.cat),
        contentDescription = null,
        contentScale = ContentScale.Crop,
        modifier = Modifier
            // Workaround to enable alpha compositing
            .graphicsLayer { alpha = 0.99f }
            .drawWithContent {
                val colors = listOf(
                    Color.Black,
                    Color.Transparent
                )
                drawContent()
                drawRect(
                    brush = Brush.horizontalGradient(colors),
                    blendMode = BlendMode.DstIn
                )
            }
    )