Is there some helper function for when you want to...
# compose
m
Is there some helper function for when you want to display a drawable resource (ID) but don’t know whether it is a vector drawable or a bitmap? In my use case, I have a drawable resId which is sometimes overridden by a vector resource, and sometimes by a bitmap.
e
sounds #compose-android specific
painterResource()
should work with vectors and bitmaps, just not other types of drawables
if you need support for all types of drawables, there is accompanist-drawablepainter
m
Thanks @ephemient So, I’ve just converted my
icon: ImageVector
args to
icon: Painter
and that seems to work well. It seems easy to convert from various image types to
Painter
. e.g.
Icons.Outlined.MailOutline
to
rememberVectorPainter(image = Icons.Outlined.MailOutline)
and resource id
painterResource(id = iconResId)
so when declaring composables, seems better to accept
Painter
than
ImageVector
or resource ids, right?
e
m
Right, yes. When we look at the
Icon
composables they all call through to the one that takes a
Painter
, so yes that seems like a good general option, Fun fact:
painterResource
detects whether the resource ID is a vector vs bitmap simply by checking whether the path ends in
.xml
! https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/c[…]tlin/androidx/compose/ui/res/PainterResources.android.kt