Using Coil, I want to in case I got no internet or...
# compose
s
Using Coil, I want to in case I got no internet or whatever to simply fill my image with a simple androidx.compose.ui.graphics.Color. Now, the .placeholder and .error function on Coil’s builder take in a
Drawable
, but I can’t figure out a way to get a hold of a
Drawable
in a simple way in Compose without going for xml drawables or something like that. Is there some API I’m missing?
c
there is a
ColorDrawable
s
Right, I can use this and provide it a color defined in my xml resources. I guess this will do for now, but if I were to have a pure compose app and my theme was specified in code, are there no alternatives? 🤔
c
the compose
Color
has a
toARGB
function that you can pass to the constructor of the
ColorDrawable
s
Aha! The
int
parameter which is annotated with
@ColorInt
means this ColorInt. Which reads
Denotes that the annotated element represents a packed color int, AARRGGBB. If applied to an int array, every element in the array represents a color integer.
I thought it was referring to an xml source! Thanks a lot! This seems to be exactly what I was looking for.
c
👍
a
IIRC Coil Compose has a
placeholder
parameter and you can use a
ColorPainter
.
s
Yeah I was still using
rememberAsyncImagePainter
as I was migrating from 1.x but you’re right I now tried this with the
AsyncImage
API which does take a
Painter
and that is super straightforward to use. I can just do
ColorPainter(hedvigBlack)
. And I had a
BitmapDrawable
I was drawing before, but now I can just do
BitmapPainter(myBitmapDrawable.bitmap.asImageBitmap())
and that works just fine too, thanks a lot!