'Create drawable programatticly in jetpack compose
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.
Solution 1:[1]
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
Solution 2:[2]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Johann |
Solution 2 | Mike |