Skip to content

Commit 1e0aa76

Browse files
author
liujianyue
committed
Refactor some line and call TypedArray.recycle in SVGAImageView
1 parent 67e385d commit 1e0aa76

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

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

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ package com.opensource.svgaplayer
33
import android.animation.Animator
44
import android.animation.ValueAnimator
55
import android.content.Context
6-
import android.graphics.Bitmap
76
import android.graphics.Canvas
87
import android.graphics.ColorFilter
98
import android.graphics.PixelFormat
109
import android.graphics.drawable.Drawable
1110
import android.os.Build
12-
import android.text.TextPaint
1311
import android.util.AttributeSet
14-
import android.view.Choreographer
1512
import android.view.View
16-
import android.view.ViewPropertyAnimator
1713
import android.view.animation.LinearInterpolator
1814
import android.widget.ImageView
1915
import java.net.URL
20-
import java.util.*
2116

2217
/**
2318
* Created by cuiminghui on 2017/3/29.
@@ -127,48 +122,39 @@ open class SVGAImageView : ImageView {
127122
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)
128123
clearsAfterStop = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterStop, true)
129124
val antiAlias = typedArray.getBoolean(R.styleable.SVGAImageView_antiAlias, true)
125+
val autoPlay = typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)
126+
typedArray.getString(R.styleable.SVGAImageView_fillMode)?.let {
127+
if (it == "0") {
128+
fillMode = FillMode.Backward
129+
}
130+
else if (it == "1") {
131+
fillMode = FillMode.Forward
132+
}
133+
}
130134
typedArray.getString(R.styleable.SVGAImageView_source)?.let {
131135
val parser = SVGAParser(context)
132136
Thread({
133-
if(it.startsWith("http://") || it.startsWith("https://")) {
134-
URL(it)?.let {
135-
parser.parse(it, object : SVGAParser.ParseCompletion {
136-
override fun onComplete(videoItem: SVGAVideoEntity) {
137-
handler?.post {
138-
videoItem.antiAlias = antiAlias
139-
setVideoItem(videoItem)
140-
if (typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)) {
141-
startAnimation()
142-
}
143-
}
144-
}
145-
override fun onError() { }
146-
})
147-
return@Thread
148-
}
149-
}
150-
parser.parse(it, object : SVGAParser.ParseCompletion {
137+
val callback: SVGAParser.ParseCompletion = object : SVGAParser.ParseCompletion {
151138
override fun onComplete(videoItem: SVGAVideoEntity) {
152139
handler?.post {
153140
videoItem.antiAlias = antiAlias
154141
setVideoItem(videoItem)
155-
if (typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)) {
142+
if (autoPlay) {
156143
startAnimation()
157144
}
158145
}
159146
}
160-
override fun onError() { }
161-
})
147+
148+
override fun onError() {}
149+
}
150+
if(it.startsWith("http://") || it.startsWith("https://")) {
151+
parser.parse(URL(it), callback)
152+
} else {
153+
parser.parse(it, callback)
154+
}
162155
}).start()
163156
}
164-
typedArray.getString(R.styleable.SVGAImageView_fillMode)?.let {
165-
if (it == "0") {
166-
fillMode = FillMode.Backward
167-
}
168-
else if (it == "1") {
169-
fillMode = FillMode.Forward
170-
}
171-
}
157+
typedArray.recycle()
172158
}
173159

174160
fun startAnimation() {
@@ -186,10 +172,11 @@ open class SVGAImageView : ImageView {
186172
val endFrame = Math.min(it.frames - 1, ((range?.location ?: 0) + (range?.length ?: Int.MAX_VALUE) - 1))
187173
val animator = ValueAnimator.ofInt(startFrame, endFrame)
188174
try {
189-
Class.forName("android.animation.ValueAnimator")?.let {
175+
val animatorClass = Class.forName("android.animation.ValueAnimator")
176+
animatorClass?.let {
190177
it.getDeclaredField("sDurationScale")?.let {
191178
it.isAccessible = true
192-
it.getFloat(Class.forName("android.animation.ValueAnimator"))?.let {
179+
it.getFloat(animatorClass).let {
193180
durationScale = it.toDouble()
194181
}
195182
}

0 commit comments

Comments
 (0)