0

I want to use rectangle with rounded corner for placeholder in coil in jetpack compose. I need use different colors for placeholder depends on my algorithm. Therefore i can't use drawable from resources. Coil require drawable for placeholder. But i don't understand how create programatticly shape drawable in jetpack compose. I will be glade for any suggestions.

Anton111111
  • 656
  • 1
  • 7
  • 23
  • You could use an xml drawable resource and then tint it programmatically, rather than creating it from scratch. – Ben P. Nov 07 '21 at 18:23
  • Does this answer your question? [How to show a custom composable placeholder using Coil in Jetpack Compose?](https://stackoverflow.com/questions/69818479/how-to-show-a-custom-composable-placeholder-using-coil-in-jetpack-compose) – Phil Dukhov Nov 07 '21 at 18:46

2 Answers2

0

You can create a drawable using the older view system and then use that with AndroidView to integrate the drawable into your coil builder for its placeholder. Here's an example of how to create a bitmap and integrate it into Compose. While this is a bitmap, you can use the same technique to create a drawable. Alternatively, you can draw a bitmap and convert it to a drawable:

https://proandroiddev.com/create-bitmaps-from-jetpack-composables-bdb2c95db51

Johann
  • 27,536
  • 39
  • 165
  • 279
0

Solution:

val context = LocalContext.current
val placeholderColor = Color(0xFFD2D2D9)
Image(
    painter = rememberImagePainter(
        data = url,
        builder = {
            val drawable = ContextCompat.getDrawable(context, R.drawable.image_round_placeholder)
            drawable?.setTint(placeholderColor.toArgb())
            placeholder(drawable)
        }
    ),
    contentDescription = null,
    modifier = Modifier.size(40.dp)
)

Result:

enter image description here

Mike
  • 321
  • 4
  • 10