Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Support adjust the speed of animation by scaling duration #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
android:layout_height="match_parent"
android:background="#000000">

<!--slow down half,if set durationScaleRatio = 5,means to accelerate 5 times-->
<com.opensource.svgaplayer.SVGAImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scaleType="fitCenter"
app:source="angel.svga"
app:durationScaleRatio="0.5"
app:antiAlias="true"/>

</RelativeLayout>
10 changes: 10 additions & 0 deletions library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ open class SVGAImageView : ImageView {

var fillMode: FillMode = FillMode.Forward

// =0 means default speed,>1 speed up,<1 Slow down
var durationScaleRatio: Float = 0f

var callback: SVGACallback? = null

private var animator: ValueAnimator? = null
Expand Down Expand Up @@ -73,6 +76,7 @@ open class SVGAImageView : ImageView {
private fun loadAttrs(attrs: AttributeSet) {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)
durationScaleRatio = typedArray.getFloat(R.styleable.SVGAImageView_durationScaleRatio, 0f) //new attr to adjust speed of animation
clearsAfterStop = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterStop, true)
val antiAlias = typedArray.getBoolean(R.styleable.SVGAImageView_antiAlias, true)
val autoPlay = typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)
Expand Down Expand Up @@ -143,6 +147,12 @@ open class SVGAImageView : ImageView {
} catch (e: Exception) {}
animator.interpolator = LinearInterpolator()
animator.duration = ((endFrame - startFrame + 1) * (1000 / it.FPS) / durationScale).toLong()

//Adjust the duration,and no need to modify the .svga file.
if (durationScaleRatio > 0) {
animator.duration = (animator.duration / durationScaleRatio).toLong()
}

animator.repeatCount = if (loops <= 0) 99999 else loops - 1
animator.addUpdateListener {
drawable.currentFrame = animator.animatedValue as Int
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<attr name="antiAlias" format="boolean" />
<attr name="loopCount" format="integer" />
<attr name="clearsAfterStop" format="boolean" />
<attr name="durationScaleRatio" format="float" />
<attr name="fillMode" format="enum">
<enum name="Backward" value="0" />
<enum name="Forward" value="1" />
Expand Down