Browse Source
The PR adds a method to convert a bitmap bytearray to the ImageBitma, a vector XML content bytearray to the ImageVector in the common code. And the same for an SVG files on non-android targets. <!-- Optional --> Fixes https://youtrack.jetbrains.com/issue/CMP-3869 Fixes https://youtrack.jetbrains.com/issue/CMP-4925 ## Release Notes ### Features - Resources - Added utility functions to decode `Bitmap ByteArray as ImageVector` and `XML ByteArray as ImageVector` in the common code and `SVG ByteArray as Painter` in the non-android codepjBooms/synchronized-object
Konstantin
4 months ago
committed by
GitHub
2 changed files with 48 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||||||
|
package org.jetbrains.compose.resources |
||||||
|
|
||||||
|
import androidx.compose.ui.graphics.ImageBitmap |
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector |
||||||
|
import androidx.compose.ui.unit.Density |
||||||
|
import org.jetbrains.compose.resources.vector.toImageVector |
||||||
|
|
||||||
|
/** |
||||||
|
* Decodes a byte array of a Bitmap to an ImageBitmap. Supports JPEG, PNG, BMP, WEBP |
||||||
|
* |
||||||
|
* Different platforms can support additional formats. |
||||||
|
* |
||||||
|
* @return The converted ImageBitmap. |
||||||
|
*/ |
||||||
|
@ExperimentalResourceApi |
||||||
|
fun ByteArray.decodeToImageBitmap(): ImageBitmap { |
||||||
|
val dumbDensity = 0 //any equal source and target density disable scaling here |
||||||
|
return this.toImageBitmap(dumbDensity, dumbDensity) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Decodes a byte array of a vector XML file to an ImageVector. |
||||||
|
* |
||||||
|
* @param density density to apply during converting the source units to the [ImageVector] units. |
||||||
|
* |
||||||
|
* @return The converted ImageVector. |
||||||
|
*/ |
||||||
|
@ExperimentalResourceApi |
||||||
|
fun ByteArray.decodeToImageVector(density: Density): ImageVector { |
||||||
|
return this.toXmlElement().toImageVector(density) |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
package org.jetbrains.compose.resources |
||||||
|
|
||||||
|
import androidx.compose.ui.graphics.painter.Painter |
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector |
||||||
|
import androidx.compose.ui.unit.Density |
||||||
|
|
||||||
|
/** |
||||||
|
* Decodes a byte array of an SVG file to a compose Painter. |
||||||
|
* |
||||||
|
* @param density density to apply during converting the source units to the [Painter] units. |
||||||
|
* |
||||||
|
* @return The converted Painter. |
||||||
|
*/ |
||||||
|
@ExperimentalResourceApi |
||||||
|
fun ByteArray.decodeToSvgPainter(density: Density): Painter { |
||||||
|
return this.toSvgElement().toSvgPainter(density) |
||||||
|
} |
Loading…
Reference in new issue