Multiplatform Resources SVG

中文
A Kotlin Multiplatform library that provides a unified API for rendering SVG resources, with platform-specific optimizations.
Features
- Android: Leverages the AndroidSVG library for robust SVG rendering and implements an asynchronous cache to improve performance.
- Other Platforms (iOS, JVM, JS, Wasm): Utilizes the standard Compose Multiplatform
painterResource for seamless integration.
Installation
Add the dependency to your build.gradle.kts (commonMain):
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.loshine:multiplatform-resources-svg:{latest_version}")
}
}
}
Usage
Use rememberSvgPainter to load and render your SVG resources. This function acts as a drop-in replacement for painterResource when working with SVGs, ensuring optimal handling on Android.
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import io.github.loshine.svg.rememberSvgPainter
import kotlinproject.composeapp.generated.resources.Res
import kotlinproject.composeapp.generated.resources.my_icon
@Composable
fun MyIcon() {
Image(
painter = rememberSvgPainter(Res.drawable.my_icon),
contentDescription = "My Icon"
)
}
SvgImage
Alternatively, you can use the SvgImage composable directly, which wraps Image and rememberSvgPainter for convenience:
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.github.loshine.svg.SvgImage
import kotlinproject.composeapp.generated.resources.Res
import kotlinproject.composeapp.generated.resources.my_icon
@Composable
fun MyIcon() {
SvgImage(
resource = Res.drawable.my_icon,
contentDescription = ,
modifier = Modifier.size(dp)
)
}
Supported Targets
- Android
- iOS
- JVM (Desktop)
- JS
- Wasm (JS)
Acknowledgments
This library wouldn't be possible without the following amazing open-source project:
- AndroidSVG: A powerful SVG rendering library for Android.
License
Apache License 2.0