Skip to content

Commit 9f25e67

Browse files
committed
feat: 新增 FillMode#Clear 属性,用于播放完毕后清空画布,并把 clearAfterStop 标记为不推荐使用
1 parent b01174e commit 9f25e67

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.annotation.SuppressLint
66
import android.content.Context
77
import android.os.Build
88
import android.util.AttributeSet
9-
import android.util.Log
109
import android.view.MotionEvent
1110
import android.view.View
1211
import android.view.animation.LinearInterpolator
@@ -19,22 +18,32 @@ import java.net.URL
1918
/**
2019
* Created by PonyCui on 2017/3/29.
2120
*/
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) {
2426

2527
private val TAG = "SVGAImageView"
2628

2729
enum class FillMode {
2830
Backward,
2931
Forward,
32+
Clear,
3033
}
3134

3235
var isAnimating = false
3336
private set
3437

3538
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
3847
var fillMode: FillMode = FillMode.Forward
3948
var callback: SVGACallback? = null
4049

@@ -57,15 +66,21 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
5766
private fun loadAttrs(attrs: AttributeSet) {
5867
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
5968
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)
6271
mAntiAlias = typedArray.getBoolean(R.styleable.SVGAImageView_antiAlias, true)
6372
mAutoPlay = typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)
6473
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+
}
6984
}
7085
}
7186
typedArray.getString(R.styleable.SVGAImageView_source)?.let {
@@ -178,16 +193,19 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
178193
isAnimating = false
179194
stopAnimation()
180195
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+
}
186207
}
187208
}
188-
if (clearsAfterStop && (animation as ValueAnimator).repeatCount <= 0) {
189-
clear()
190-
}
191209
callback?.onFinished()
192210
}
193211

@@ -274,7 +292,7 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
274292

275293
override fun onDetachedFromWindow() {
276294
super.onDetachedFromWindow()
277-
stopAnimation(true)
295+
stopAnimation(clearsAfterDetached)
278296
if (clearsAfterDetached) {
279297
clear()
280298
}

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<attr name="fillMode" format="enum">
1111
<enum name="Backward" value="0" />
1212
<enum name="Forward" value="1" />
13+
<enum name="Clear" value="2"/>
1314
</attr>
1415
</declare-styleable>
1516
</resources>

0 commit comments

Comments
 (0)