Skip to content

Commit ba6eed6

Browse files
committed
add fun startAnimation(range: SVGARange?, reverse: Boolean = false)
1 parent c728377 commit ba6eed6

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.0'
10+
classpath 'com.android.tools.build:gradle:3.0.1'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1212

1313
// NOTE: Do not place your application dependencies here; they belong

library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,18 @@ open class SVGAImageView : ImageView {
172172
}
173173

174174
fun startAnimation() {
175+
startAnimation(null, false)
176+
}
177+
178+
fun startAnimation(range: SVGARange?, reverse: Boolean = false) {
175179
val drawable = drawable as? SVGADrawable ?: return
176180
drawable.cleared = false
177181
drawable.scaleType = scaleType
178182
drawable.videoItem?.let {
179183
var durationScale = 1.0
180-
val animator = ValueAnimator.ofInt(0, it.frames - 1)
184+
val startFrame = Math.max(0, range?.location ?: 0)
185+
val endFrame = Math.min(it.frames - 1, ((range?.location ?: 0) + (range?.length ?: Int.MAX_VALUE) - 1))
186+
val animator = ValueAnimator.ofInt(startFrame, endFrame)
181187
try {
182188
Class.forName("android.animation.ValueAnimator")?.let {
183189
it.getDeclaredField("sDurationScale")?.let {
@@ -189,7 +195,7 @@ open class SVGAImageView : ImageView {
189195
}
190196
} catch (e: Exception) {}
191197
animator.interpolator = LinearInterpolator()
192-
animator.duration = (it.frames * (1000 / it.FPS) / durationScale).toLong()
198+
animator.duration = ((endFrame - startFrame + 1) * (1000 / it.FPS) / durationScale).toLong()
193199
animator.repeatCount = if (loops <= 0) 99999 else loops - 1
194200
animator.addUpdateListener {
195201
drawable.currentFrame = animator.animatedValue as Int
@@ -204,7 +210,10 @@ open class SVGAImageView : ImageView {
204210
stopAnimation()
205211
if (!clearsAfterStop) {
206212
if (fillMode == FillMode.Backward) {
207-
drawable.currentFrame = 0
213+
drawable.currentFrame = startFrame
214+
}
215+
else if (fillMode == FillMode.Forward) {
216+
drawable.currentFrame = endFrame
208217
}
209218
}
210219
callback?.onFinished()
@@ -216,7 +225,12 @@ open class SVGAImageView : ImageView {
216225
isAnimating = true
217226
}
218227
})
219-
animator.start()
228+
if (reverse) {
229+
animator.reverse()
230+
}
231+
else {
232+
animator.start()
233+
}
220234
this.animator = animator
221235
}
222236
}

library/src/main/java/com/opensource/svgaplayer/SVGAStructs.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ package com.opensource.svgaplayer
66

77
class SVGAPoint(val x: Float, val y: Float, val value: Float)
88

9-
class SVGARect(val x: Double, val y: Double, val width: Double, val height: Double)
9+
class SVGARect(val x: Double, val y: Double, val width: Double, val height: Double)
10+
11+
class SVGARange(val location: Int, val length: Int)

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ HttpResponseCache.install(cacheDir, 1024 * 1024 * 128)
149149

150150
### Methods
151151
* startAnimation() - Play Animation from 0 frame.
152+
* startAnimation(range: SVGARange?, reverse: Boolean = false) - Play Animation in [location, location + length] range, and reverse or not.
152153
* pauseAnimation() - Pause Animation and keep on current frame.
153154
* stopAnimation() - Stop Animation,Clears Canvas while clearsAfterStop == YES.
154155
* stepToFrame(int frame, boolean andPlay) - Step to N frame, and then Play Animation if andPlay === true.

0 commit comments

Comments
 (0)