This repository was archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathSVGAImageView.kt
248 lines (217 loc) · 8.82 KB
/
SVGAImageView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.opensource.svgaplayer
import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import com.opensource.svgaplayer.utils.SVGARange
import java.lang.ref.WeakReference
import java.net.URL
/**
* Created by PonyCui on 2017/3/29.
*/
open class SVGAImageView : ImageView {
enum class FillMode {
Backward,
Forward,
}
var isAnimating = false
private set
var loops = 0
var clearsAfterStop = true
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
constructor(context: Context?) : super(context) {
setSoftwareLayerType()
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
setSoftwareLayerType()
attrs?.let { loadAttrs(it) }
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
setSoftwareLayerType()
attrs?.let { loadAttrs(it) }
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
setSoftwareLayerType()
attrs?.let { loadAttrs(it) }
}
private fun setSoftwareLayerType() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
animator?.cancel()
animator?.removeAllListeners()
animator?.removeAllUpdateListeners()
}
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)
typedArray.getString(R.styleable.SVGAImageView_fillMode)?.let {
if (it == "0") {
fillMode = FillMode.Backward
}
else if (it == "1") {
fillMode = FillMode.Forward
}
}
typedArray.getString(R.styleable.SVGAImageView_source)?.let {
val parser = SVGAParser(context)
Thread {
val callback: SVGAParser.ParseCompletion = object : SVGAParser.ParseCompletion {
override fun onComplete(videoItem: SVGAVideoEntity) {
videoItem.antiAlias = antiAlias
setVideoItem(videoItem)
(drawable as? SVGADrawable)?.scaleType = scaleType
if (autoPlay) {
startAnimation()
}
}
}
override fun onError() {}
}
if(it.startsWith("http://") || it.startsWith("https://")) {
parser.parse(URL(it), callback)
} else {
parser.parse(it, callback)
}
}.start()
}
typedArray.recycle()
}
fun startAnimation() {
startAnimation(null, false)
}
fun startAnimation(range: SVGARange?, reverse: Boolean = false) {
stopAnimation(false)
val drawable = drawable as? SVGADrawable ?: return
drawable.cleared = false
drawable.scaleType = scaleType
drawable.videoItem.let {
var durationScale = 1.0
val startFrame = Math.max(0, range?.location ?: 0)
val endFrame = Math.min(it.frames - 1, ((range?.location ?: 0) + (range?.length ?: Int.MAX_VALUE) - 1))
val animator = ValueAnimator.ofInt(startFrame, endFrame)
try {
val animatorClass = Class.forName("android.animation.ValueAnimator")
animatorClass?.let {
it.getDeclaredField("sDurationScale")?.let {
it.isAccessible = true
it.getFloat(animatorClass).let {
durationScale = it.toDouble()
}
if (durationScale == 0.0) {
it.setFloat(animatorClass, 1.0f)
durationScale = 1.0
Log.e("SVGAPlayer", "The animation duration scale has been reset to 1.0x, because you closed it on developer options.")
}
}
}
} 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
callback?.onStep(drawable.currentFrame, ((drawable.currentFrame + 1).toDouble() / drawable.videoItem.frames.toDouble()))
}
animator.addListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) {
callback?.onRepeat()
}
override fun onAnimationEnd(animation: Animator?) {
isAnimating = false
stopAnimation()
if (!clearsAfterStop) {
if (fillMode == FillMode.Backward) {
drawable.currentFrame = startFrame
}
else if (fillMode == FillMode.Forward) {
drawable.currentFrame = endFrame
}
}
callback?.onFinished()
}
override fun onAnimationCancel(animation: Animator?) {
isAnimating = false
}
override fun onAnimationStart(animation: Animator?) {
isAnimating = true
}
})
if (reverse) {
animator.reverse()
}
else {
animator.start()
}
this.animator = animator
}
}
fun pauseAnimation() {
stopAnimation(false)
callback?.onPause()
}
fun stopAnimation() {
stopAnimation(clear = clearsAfterStop)
}
fun stopAnimation(clear: Boolean) {
animator?.cancel()
animator?.removeAllListeners()
animator?.removeAllUpdateListeners()
(drawable as? SVGADrawable)?.let {
it.cleared = clear
}
}
fun setVideoItem(videoItem: SVGAVideoEntity?) {
setVideoItem(videoItem, SVGADynamicEntity())
}
fun setVideoItem(videoItem: SVGAVideoEntity?, dynamicItem: SVGADynamicEntity?) {
if (videoItem == null) {
setImageDrawable(null)
return
}
val drawable = SVGADrawable(videoItem, dynamicItem ?: SVGADynamicEntity())
drawable.cleared = clearsAfterStop
setImageDrawable(drawable)
}
fun stepToFrame(frame: Int, andPlay: Boolean) {
pauseAnimation()
val drawable = drawable as? SVGADrawable ?: return
drawable.currentFrame = frame
if (andPlay) {
startAnimation()
animator?.let {
it.currentPlayTime = (Math.max(0.0f, Math.min(1.0f, (frame.toFloat() / drawable.videoItem.frames.toFloat()))) * it.duration).toLong()
}
}
}
fun stepToPercentage(percentage: Double, andPlay: Boolean) {
val drawable = drawable as? SVGADrawable ?: return
var frame = (drawable.videoItem.frames * percentage).toInt()
if (frame >= drawable.videoItem.frames && frame > 0) {
frame = drawable.videoItem.frames - 1
}
stepToFrame(frame, andPlay)
}
}