Chinese README: 中文文档.
AndroidSvgTranscoder makes you able to transcode svg files to java code, with which you can render svg images in canvas.
-
transcoder-core:core module that can transcode a svg file to java file。Generally, you don‘t need to use this module directly, instead using transcoder-plugin is more convenient unless you want to do some customization job. This library is published to MavenCentral.
-
transcoder-plugin:gradle plugin module, you can apply this plugin which offer a task 'svgBatchConverter' that can transcode a set of svg files at the same time. This gradle plugin is published to MavenCentral and Gradle Plugin Portal.
-
transcoder-sample:A sample module that show you how to use this transcoder-plugin。
- Make sure repository below exists in your setting.gradle。If your AGP's version is too low that doesn't support 'pluginManagement' grammar,you can add the repository in build.gradle of root project。
pluginManagement {
repositories {
mavenCentral() // or gradlePluginPortal()
google()
}
}
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
}
}
- Apply AndroidSvgTranscoder plugin:
plugins {
id 'io.github.furutuki.android-svg-transcoder' version '0.1.1'
}
- Add svgTranscoder extension in your build.gradle and config the three properties below:
svgFolder: directory that contains your svg files
javaFolder: directory that contains java files generated
packageName : package name of the java files generated
svgTranscoder {
svgFolder = "${projectDir}/svg"
javaFolder = "${projectDir}/src/main/java/com/github/furutuki/androidsvgtranscoder/sample/svgobj"
packageName = "com.github.furutuki.androidsvgtranscoder.sample.svgobj"
}
- execute the task svgBatchConverter:
./gradlew svgBatchConverter
The task execution result will be showed in the terminal, if succeeded, you will find java files in the specified folder.
Each java file represent a svg object, you can new a object of it, and invoke paint() method to render the svg like this:
val arrowDown = ArrowDown()
arrowDown.paint(canvas, 100, 80, 300, 10)
The transcoder-sample module has some svg files in directory svg,execute the task svgBatchConverter and use the output java files to draw them, below is the screenshot of the sample project.