@@ -3,21 +3,16 @@ package com.opensource.svgaplayer
3
3
import android.animation.Animator
4
4
import android.animation.ValueAnimator
5
5
import android.content.Context
6
- import android.graphics.Bitmap
7
6
import android.graphics.Canvas
8
7
import android.graphics.ColorFilter
9
8
import android.graphics.PixelFormat
10
9
import android.graphics.drawable.Drawable
11
10
import android.os.Build
12
- import android.text.TextPaint
13
11
import android.util.AttributeSet
14
- import android.view.Choreographer
15
12
import android.view.View
16
- import android.view.ViewPropertyAnimator
17
13
import android.view.animation.LinearInterpolator
18
14
import android.widget.ImageView
19
15
import java.net.URL
20
- import java.util.*
21
16
22
17
/* *
23
18
* Created by cuiminghui on 2017/3/29.
@@ -127,48 +122,39 @@ open class SVGAImageView : ImageView {
127
122
loops = typedArray.getInt(R .styleable.SVGAImageView_loopCount , 0 )
128
123
clearsAfterStop = typedArray.getBoolean(R .styleable.SVGAImageView_clearsAfterStop , true )
129
124
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
+ }
130
134
typedArray.getString(R .styleable.SVGAImageView_source )?.let {
131
135
val parser = SVGAParser (context)
132
136
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 {
151
138
override fun onComplete (videoItem : SVGAVideoEntity ) {
152
139
handler?.post {
153
140
videoItem.antiAlias = antiAlias
154
141
setVideoItem(videoItem)
155
- if (typedArray.getBoolean( R .styleable. SVGAImageView_autoPlay , true ) ) {
142
+ if (autoPlay ) {
156
143
startAnimation()
157
144
}
158
145
}
159
146
}
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
+ }
162
155
}).start()
163
156
}
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()
172
158
}
173
159
174
160
fun startAnimation () {
@@ -186,10 +172,11 @@ open class SVGAImageView : ImageView {
186
172
val endFrame = Math .min(it.frames - 1 , ((range?.location ? : 0 ) + (range?.length ? : Int .MAX_VALUE ) - 1 ))
187
173
val animator = ValueAnimator .ofInt(startFrame, endFrame)
188
174
try {
189
- Class .forName(" android.animation.ValueAnimator" )?.let {
175
+ val animatorClass = Class .forName(" android.animation.ValueAnimator" )
176
+ animatorClass?.let {
190
177
it.getDeclaredField(" sDurationScale" )?.let {
191
178
it.isAccessible = true
192
- it.getFloat(Class .forName( " android.animation.ValueAnimator " ))? .let {
179
+ it.getFloat(animatorClass) .let {
193
180
durationScale = it.toDouble()
194
181
}
195
182
}
0 commit comments