Adding Gradle Dependency
Add the official `SVGAPlayer-Android` dependency to your app module `build.gradle.kts`:
```kotlin
dependencies {
implementation("com.github.yyued:SVGAPlayer-Android:2.6.1")
}
```
Ensure `jitpack.io` is listed in your project repository settings.
```kotlin
dependencies {
implementation("com.github.yyued:SVGAPlayer-Android:2.6.1")
}
```
Ensure `jitpack.io` is listed in your project repository settings.
XML Layout Declaration
Declare the `SVGAImageView` inside your XML layout file:
```xml
<com.opensource.svgaplayer.SVGAImageView
android:id="@+id/svgaImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:autoPlay="true"
app:loopCount="0"
app:clearsAfterStop="true" />
```
```xml
<com.opensource.svgaplayer.SVGAImageView
android:id="@+id/svgaImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:autoPlay="true"
app:loopCount="0"
app:clearsAfterStop="true" />
```
Loading Animation via Kotlin Code
Use `SVGAParser` to load from assets or a remote URL:
```kotlin
val parser = SVGAParser(context)
parser.decodeFromURL(URL("https://example.com/gift.svga"), object : SVGAParser.ParseCompletion {
override fun onComplete(videoItem: SVGAVideoEntity) {
val drawable = SVGADrawable(videoItem)
binding.svgaImageView.setImageDrawable(drawable)
binding.svgaImageView.startAnimation()
}
override fun onError() {
// Handle network or parsing error
}
})
```
```kotlin
val parser = SVGAParser(context)
parser.decodeFromURL(URL("https://example.com/gift.svga"), object : SVGAParser.ParseCompletion {
override fun onComplete(videoItem: SVGAVideoEntity) {
val drawable = SVGADrawable(videoItem)
binding.svgaImageView.setImageDrawable(drawable)
binding.svgaImageView.startAnimation()
}
override fun onError() {
// Handle network or parsing error
}
})
```