https://kotlinlang.org logo
#compose
Title
# compose
c

chanjungskim

03/03/2023, 1:28 AM
I created custom button with coil. But the preview isn't working. How can I make it displayable in preview?
Copy code
fun GifImage(
    @DrawableRes resId: Int,
    contentDescription: String,
    modifier: Modifier = Modifier,
) {
    val imageLoader = ImageLoader.Builder(LocalContext.current)
        .components {
            if (SDK_INT >= 28) {
                add(ImageDecoderDecoder.Factory())
            } else {
                add(GifDecoder.Factory())
            }
        }.build()
    Image(
        painter = rememberAsyncImagePainter(resId, imageLoader),
        contentDescription = contentDescription,
        modifier = modifier
    )
}
c

Colin White

03/03/2023, 3:22 AM
Coil only displays the
placeholder
element in preview since the preview environment is a bit special. IIRC certain decoders won’t work inside preview
Also I’d
remember
the
ImageLoader
so it’s not recreated on each composition
s

Stylianos Gakis

10/20/2023, 3:38 PM
Is this still the case? Would it not be nice to be able to provide a preview only ImageLoader, something like this
class PreviewImageLoader(private val drawable: Drawable) : ImageLoader {...
to your previews to do something like render all the preview images with a filled red color or something like that, just to know a but better what the final UI will look like before running the app?
9 Views