Hello! I am trying to load a layer-list drawable w...
# compose
v
Hello! I am trying to load a layer-list drawable with this code:
Copy code
Image(
    painter = painterResource(id = someId),
    contentDescription = null
)
but i get this error:
Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG
. I can load vector drawables so I suppose that I need to load layer-list differently... How is this possible?
m
If you are getting this message, you're not loading a vector drawable. You are loading an XML drawable (like a layer list or some other thing). Here's a quick utlity function that will load any drawable and give you a compose ImageBitmap instance:
Copy code
@Composable
fun loadXmlDrawable(@DrawableRes resId: Int): ImageBitmap? =
    ContextCompat.getDrawable(
        LocalContext.current,
        resId
    )?.toBitmap()?.asImageBitmap()
you should be able to pass the ImageBitmap to the Image composable.
What's not clear to me though is how it will evaluate things like selection state if you have selectors in there. Also of note is that if your drawable uses theme attributes, you'll have to make sure your current context either has the appropriate theme, or you wrap it with a ThemeContextWrapper.
I suspect these are some of the reasons why this functionality isn't just built in.
c
Yeah I don’t think layer list drawables are supported in Compose, but maybe @Nader Jawad or @Louis Pullen-Freilich [G] would know
n
We explicitly don't support all drawables in compose. The rationale is similar to what @mattinger has suggested above. Drawables have other facilities coupled with them such as selection state or other state properties that are specific to the implementation of the drawable and are not reactive development friendly. If you would like to recreate the functionality of a layer list you can use a custom drawmodifier and issue drawing commands within the DrawScope lambda. Or you can create a custom painter instance that understands your specific use case.
👍 2
v
Ok! I understand. I have not tried yet the
loadXmlDrawable
, but probably this is the solution. Thank you very much all of you! :)
598 Views