|
|
@ -56,10 +56,17 @@ private val emptyImageBitmap: ImageBitmap by lazy { ImageBitmap(1, 1) } |
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
fun imageResource(resource: DrawableResource): ImageBitmap { |
|
|
|
fun imageResource(resource: DrawableResource): ImageBitmap { |
|
|
|
val resourceReader = LocalResourceReader.currentOrPreview |
|
|
|
val resourceReader = LocalResourceReader.currentOrPreview |
|
|
|
val imageBitmap by rememberResourceState(resource, resourceReader, { emptyImageBitmap }) { env -> |
|
|
|
val resourceEnvironment = rememberResourceEnvironment() |
|
|
|
val path = resource.getResourceItemByEnvironment(env).path |
|
|
|
val imageBitmap by rememberResourceState( |
|
|
|
val cached = loadImage(path, resourceReader) { |
|
|
|
resource, resourceReader, resourceEnvironment, { emptyImageBitmap } |
|
|
|
ImageCache.Bitmap(it.toImageBitmap()) |
|
|
|
) { env -> |
|
|
|
|
|
|
|
val item = resource.getResourceItemByEnvironment(env) |
|
|
|
|
|
|
|
val resourceDensityQualifier = item.qualifiers.firstOrNull { it is DensityQualifier } as? DensityQualifier |
|
|
|
|
|
|
|
val resourceDensity = resourceDensityQualifier?.dpi ?: DensityQualifier.MDPI.dpi |
|
|
|
|
|
|
|
val screenDensity = resourceEnvironment.density.dpi |
|
|
|
|
|
|
|
val path = item.path |
|
|
|
|
|
|
|
val cached = loadImage(path, "$path-${screenDensity}dpi", resourceReader) { |
|
|
|
|
|
|
|
ImageCache.Bitmap(it.toImageBitmap(resourceDensity, screenDensity)) |
|
|
|
} as ImageCache.Bitmap |
|
|
|
} as ImageCache.Bitmap |
|
|
|
cached.bitmap |
|
|
|
cached.bitmap |
|
|
|
} |
|
|
|
} |
|
|
@ -82,7 +89,7 @@ fun vectorResource(resource: DrawableResource): ImageVector { |
|
|
|
val density = LocalDensity.current |
|
|
|
val density = LocalDensity.current |
|
|
|
val imageVector by rememberResourceState(resource, resourceReader, density, { emptyImageVector }) { env -> |
|
|
|
val imageVector by rememberResourceState(resource, resourceReader, density, { emptyImageVector }) { env -> |
|
|
|
val path = resource.getResourceItemByEnvironment(env).path |
|
|
|
val path = resource.getResourceItemByEnvironment(env).path |
|
|
|
val cached = loadImage(path, resourceReader) { |
|
|
|
val cached = loadImage(path, path, resourceReader) { |
|
|
|
ImageCache.Vector(it.toXmlElement().toImageVector(density)) |
|
|
|
ImageCache.Vector(it.toXmlElement().toImageVector(density)) |
|
|
|
} as ImageCache.Vector |
|
|
|
} as ImageCache.Vector |
|
|
|
cached.vector |
|
|
|
cached.vector |
|
|
@ -102,7 +109,7 @@ private fun svgPainter(resource: DrawableResource): Painter { |
|
|
|
val density = LocalDensity.current |
|
|
|
val density = LocalDensity.current |
|
|
|
val svgPainter by rememberResourceState(resource, resourceReader, density, { emptySvgPainter }) { env -> |
|
|
|
val svgPainter by rememberResourceState(resource, resourceReader, density, { emptySvgPainter }) { env -> |
|
|
|
val path = resource.getResourceItemByEnvironment(env).path |
|
|
|
val path = resource.getResourceItemByEnvironment(env).path |
|
|
|
val cached = loadImage(path, resourceReader) { |
|
|
|
val cached = loadImage(path, path, resourceReader) { |
|
|
|
ImageCache.Svg(it.toSvgElement().toSvgPainter(density)) |
|
|
|
ImageCache.Svg(it.toSvgElement().toSvgPainter(density)) |
|
|
|
} as ImageCache.Svg |
|
|
|
} as ImageCache.Svg |
|
|
|
cached.painter |
|
|
|
cached.painter |
|
|
@ -126,7 +133,7 @@ suspend fun getDrawableResourceBytes( |
|
|
|
return DefaultResourceReader.read(resourceItem.path) |
|
|
|
return DefaultResourceReader.read(resourceItem.path) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
internal expect fun ByteArray.toImageBitmap(): ImageBitmap |
|
|
|
internal expect fun ByteArray.toImageBitmap(resourceDensity: Int, targetDensity: Int): ImageBitmap |
|
|
|
internal expect fun ByteArray.toXmlElement(): Element |
|
|
|
internal expect fun ByteArray.toXmlElement(): Element |
|
|
|
internal expect fun ByteArray.toSvgElement(): SvgElement |
|
|
|
internal expect fun ByteArray.toSvgElement(): SvgElement |
|
|
|
|
|
|
|
|
|
|
@ -145,6 +152,7 @@ internal fun dropImageCache() { |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun loadImage( |
|
|
|
private suspend fun loadImage( |
|
|
|
path: String, |
|
|
|
path: String, |
|
|
|
|
|
|
|
cacheKey: String, |
|
|
|
resourceReader: ResourceReader, |
|
|
|
resourceReader: ResourceReader, |
|
|
|
decode: (ByteArray) -> ImageCache |
|
|
|
decode: (ByteArray) -> ImageCache |
|
|
|
): ImageCache = imageCache.getOrLoad(path) { decode(resourceReader.read(path)) } |
|
|
|
): ImageCache = imageCache.getOrLoad(cacheKey) { decode(resourceReader.read(path)) } |
|
|
|