@@ -6,7 +6,6 @@ import android.annotation.SuppressLint
6
6
import android.content.Context
7
7
import android.os.Build
8
8
import android.util.AttributeSet
9
- import android.util.Log
10
9
import android.view.MotionEvent
11
10
import android.view.View
12
11
import android.view.animation.LinearInterpolator
@@ -19,22 +18,32 @@ import java.net.URL
19
18
/* *
20
19
* Created by PonyCui on 2017/3/29.
21
20
*/
22
- open class SVGAImageView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = 0 )
23
- : ImageView (context, attrs, defStyleAttr) {
21
+ open class SVGAImageView @JvmOverloads constructor(
22
+ context : Context ,
23
+ attrs : AttributeSet ? = null ,
24
+ defStyleAttr : Int = 0
25
+ ) : ImageView(context, attrs, defStyleAttr) {
24
26
25
27
private val TAG = " SVGAImageView"
26
28
27
29
enum class FillMode {
28
30
Backward ,
29
31
Forward ,
32
+ Clear ,
30
33
}
31
34
32
35
var isAnimating = false
33
36
private set
34
37
35
38
var loops = 0
36
- var clearsAfterStop = true
37
- var clearsAfterDetached = true
39
+
40
+ @Deprecated(
41
+ " It is recommended to use clearAfterDetached, or manually call to SVGAVideoEntity#clear." +
42
+ " If you just consider cleaning up the canvas after playing, you can use FillMode#Clear." ,
43
+ level = DeprecationLevel .WARNING
44
+ )
45
+ var clearsAfterStop = false
46
+ var clearsAfterDetached = false
38
47
var fillMode: FillMode = FillMode .Forward
39
48
var callback: SVGACallback ? = null
40
49
@@ -57,15 +66,21 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
57
66
private fun loadAttrs (attrs : AttributeSet ) {
58
67
val typedArray = context.theme.obtainStyledAttributes(attrs, R .styleable.SVGAImageView , 0 , 0 )
59
68
loops = typedArray.getInt(R .styleable.SVGAImageView_loopCount , 0 )
60
- clearsAfterStop = typedArray.getBoolean(R .styleable.SVGAImageView_clearsAfterStop , true )
61
- clearsAfterDetached = typedArray.getBoolean(R .styleable.SVGAImageView_clearsAfterDetached , true )
69
+ clearsAfterStop = typedArray.getBoolean(R .styleable.SVGAImageView_clearsAfterStop , false )
70
+ clearsAfterDetached = typedArray.getBoolean(R .styleable.SVGAImageView_clearsAfterDetached , false )
62
71
mAntiAlias = typedArray.getBoolean(R .styleable.SVGAImageView_antiAlias , true )
63
72
mAutoPlay = typedArray.getBoolean(R .styleable.SVGAImageView_autoPlay , true )
64
73
typedArray.getString(R .styleable.SVGAImageView_fillMode )?.let {
65
- if (it == " 0" ) {
66
- fillMode = FillMode .Backward
67
- } else if (it == " 1" ) {
68
- fillMode = FillMode .Forward
74
+ when (it) {
75
+ " 0" -> {
76
+ fillMode = FillMode .Backward
77
+ }
78
+ " 1" -> {
79
+ fillMode = FillMode .Forward
80
+ }
81
+ " 2" -> {
82
+ fillMode = FillMode .Clear
83
+ }
69
84
}
70
85
}
71
86
typedArray.getString(R .styleable.SVGAImageView_source )?.let {
@@ -178,16 +193,19 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
178
193
isAnimating = false
179
194
stopAnimation()
180
195
val drawable = getSVGADrawable()
181
- if (! clearsAfterStop && drawable != null ) {
182
- if (fillMode == FillMode .Backward ) {
183
- drawable.currentFrame = mStartFrame
184
- } else if (fillMode == FillMode .Forward ) {
185
- drawable.currentFrame = mEndFrame
196
+ if (drawable != null ) {
197
+ when (fillMode) {
198
+ FillMode .Backward -> {
199
+ drawable.currentFrame = mStartFrame
200
+ }
201
+ FillMode .Forward -> {
202
+ drawable.currentFrame = mEndFrame
203
+ }
204
+ FillMode .Clear -> {
205
+ drawable.cleared = true
206
+ }
186
207
}
187
208
}
188
- if (clearsAfterStop && (animation as ValueAnimator ).repeatCount <= 0 ) {
189
- clear()
190
- }
191
209
callback?.onFinished()
192
210
}
193
211
@@ -274,7 +292,7 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
274
292
275
293
override fun onDetachedFromWindow () {
276
294
super .onDetachedFromWindow()
277
- stopAnimation(true )
295
+ stopAnimation(clearsAfterDetached )
278
296
if (clearsAfterDetached) {
279
297
clear()
280
298
}
0 commit comments