Reputation: 60081
I was using jetpack compose 1.0.0-alpha 11. The below code works.
Image(
imageResource(R.drawable.header),
contentDescription = null,
modifier = itemModifier,
contentScale = ContentScale.Crop
)
However in jetpack compose 1.0.0-beta07, imageResource
and vectorResource
no longer works.
What's the new way of getting the image resources?
Upvotes: 6
Views: 5404
Reputation: 363479
With 1.0.x
you can use the painterResource
function:
Image(painterResource(R.drawable.ic_xxxx),"content description")
This can load either an instance of BitmapPainter
or VectorPainter
for ImageBitmap
based assets or vector based assets respectively.
This method works with images (PNG or JPG files) or VectorDrawable
xml assets.
About the usage of ImageBitmap.imageResource(R.drawable.header)
This function is intended to be used for when low-level
ImageBitmap
-specific functionality is required. For simply displaying onscreen, the vector/bitmap-agnosticpainterResource
is recommended instead.
Upvotes: 7
Reputation: 60081
I found we can continue to use vectorResource
and imageResource
using the extension of ImageVector
and ImageBitmap
e.g.
Image(
ImageBitmap.imageResource(R.drawable.header),
contentDescription = null,
modifier = itemModifier,
contentScale = ContentScale.Crop
)
Upvotes: 4
Reputation:
You can easily access drawable resouces by using painterResource.
Image(painter = painterResource(id = R.drawable.someVector), contentDescription = null)
Upvotes: 2