@@ -6,10 +6,13 @@ import android.graphics.*
6
6
import android.net.Uri
7
7
import android.widget.ImageView
8
8
import org.jetbrains.anko.doAsync
9
+ import org.jetbrains.anko.image
9
10
import org.jetbrains.anko.runOnUiThread
10
11
import org.jetbrains.anko.uiThread
11
- import oupson.apng.Utils.Companion.isApng
12
+ import oupson.apng.utils. Utils.Companion.isApng
12
13
import oupson.apng.exceptions.NotApngException
14
+ import oupson.apng.utils.ApngAnimatorOptions
15
+ import oupson.apng.utils.Utils
13
16
import java.io.File
14
17
import java.net.URL
15
18
@@ -41,6 +44,7 @@ class ApngAnimator(private val context: Context) {
41
44
private var doOnLoaded : (ApngAnimator ) -> Unit = {}
42
45
private var AnimationLoopListener : () -> Unit = {}
43
46
private var duration : ArrayList <Float >? = null
47
+ private var scaleType : ImageView .ScaleType ? = null
44
48
45
49
var isApng = false
46
50
var loadNotApng = true
@@ -74,7 +78,7 @@ class ApngAnimator(private val context: Context) {
74
78
* @param speed The speed
75
79
* @throws NotApngException
76
80
*/
77
- fun load (file : File , speed : Float? = null) {
81
+ fun load (file : File , speed : Float? = null, apngAnimatorOptions : ApngAnimatorOptions ? = null ) {
78
82
doAsync {
79
83
val bytes = file.readBytes()
80
84
if (isApng(bytes)) {
@@ -84,10 +88,12 @@ class ApngAnimator(private val context: Context) {
84
88
APNGDisassembler .disassemble(file.readBytes()).frames.apply {
85
89
draw(this )
86
90
}
91
+ scaleType = apngAnimatorOptions?.scaleType
87
92
setupAnimationDrawableAndStart()
88
93
} else {
89
94
if (loadNotApng) {
90
95
context.runOnUiThread {
96
+ imageView?.scaleType = this @ApngAnimator.scaleType ? : ImageView .ScaleType .FIT_CENTER
91
97
imageView?.setImageBitmap(BitmapFactory .decodeByteArray(bytes, 0 , bytes.size))
92
98
}
93
99
} else {
@@ -103,7 +109,7 @@ class ApngAnimator(private val context: Context) {
103
109
* @param speed The speed
104
110
* @throws NotApngException
105
111
*/
106
- fun load (uri : Uri , speed : Float? = null) {
112
+ fun load (uri : Uri , speed : Float? = null, apngAnimatorOptions : ApngAnimatorOptions ? = null ) {
107
113
doAsync {
108
114
val bytes = context.contentResolver.openInputStream(uri).readBytes()
109
115
if (isApng(bytes)) {
@@ -113,10 +119,12 @@ class ApngAnimator(private val context: Context) {
113
119
APNGDisassembler .disassemble(bytes).frames.apply {
114
120
draw(this )
115
121
}
122
+ scaleType = apngAnimatorOptions?.scaleType
116
123
setupAnimationDrawableAndStart()
117
124
} else {
118
125
if (loadNotApng) {
119
126
context.runOnUiThread {
127
+ imageView?.scaleType = this @ApngAnimator.scaleType ? : ImageView .ScaleType .FIT_CENTER
120
128
imageView?.setImageBitmap(BitmapFactory .decodeByteArray(bytes, 0 , bytes.size))
121
129
}
122
130
} else {
@@ -132,7 +140,7 @@ class ApngAnimator(private val context: Context) {
132
140
* @param speed The speed
133
141
* @throws NotApngException
134
142
*/
135
- fun loadUrl (url : URL , speed : Float? = null) {
143
+ fun loadUrl (url : URL , speed : Float? = null, apngAnimatorOptions : ApngAnimatorOptions ? = null ) {
136
144
doAsync(exceptionHandler = { e -> e.printStackTrace() }) {
137
145
this @ApngAnimator.speed = speed
138
146
// Download PNG
@@ -142,10 +150,12 @@ class ApngAnimator(private val context: Context) {
142
150
APNGDisassembler .disassemble(this ).frames.apply {
143
151
draw(this )
144
152
}
153
+ scaleType = apngAnimatorOptions?.scaleType
145
154
setupAnimationDrawableAndStart()
146
155
} else {
147
156
if (loadNotApng) {
148
157
context.runOnUiThread {
158
+ imageView?.scaleType = this @ApngAnimator.scaleType ? : ImageView .ScaleType .FIT_CENTER
149
159
imageView?.setImageBitmap(BitmapFactory .decodeByteArray(this @apply, 0 , this @apply.size))
150
160
}
151
161
} else {
@@ -165,18 +175,20 @@ class ApngAnimator(private val context: Context) {
165
175
* @param speed The speed
166
176
* @throws NotApngException
167
177
*/
168
- fun load (byteArray : ByteArray , speed : Float? = null) {
178
+ fun load (byteArray : ByteArray , speed : Float? = null, apngAnimatorOptions : ApngAnimatorOptions ? ) {
169
179
doAsync {
170
180
this @ApngAnimator.speed = speed
171
181
if (isApng(byteArray)) {
172
182
isApng = true
173
183
APNGDisassembler .disassemble(byteArray).frames.apply {
174
184
draw(this )
175
185
}
186
+ scaleType = apngAnimatorOptions?.scaleType
176
187
setupAnimationDrawableAndStart()
177
188
} else {
178
189
if (loadNotApng) {
179
190
context.runOnUiThread {
191
+ imageView?.scaleType = this @ApngAnimator.scaleType ? : ImageView .ScaleType .FIT_CENTER
180
192
imageView?.setImageBitmap(BitmapFactory .decodeByteArray(byteArray, 0 , byteArray.size))
181
193
}
182
194
} else {
@@ -192,18 +204,18 @@ class ApngAnimator(private val context: Context) {
192
204
* @param speed The speed
193
205
* @throws NotApngException
194
206
*/
195
- fun load (string : String , speed : Float? = null) {
207
+ fun load (string : String , speed : Float? = null, apngAnimatorOptions : ApngAnimatorOptions ? = null ) {
196
208
doAsync {
197
209
this @ApngAnimator.speed = speed
198
210
if (string.contains(" http" ) || string.contains(" https" )) {
199
211
val url = URL (string)
200
- loadUrl(url, speed)
212
+ loadUrl(url, speed, apngAnimatorOptions )
201
213
} else if (File (string).exists()) {
202
214
var pathToLoad = if (string.startsWith(" content://" )) string else " file://$string "
203
215
pathToLoad = pathToLoad.replace(" %" , " %25" ).replace(" #" , " %23" )
204
216
val bytes = context.contentResolver.openInputStream(Uri .parse(pathToLoad)).readBytes()
205
217
if (isApng(bytes)) {
206
- load(bytes, speed)
218
+ load(bytes, speed, apngAnimatorOptions )
207
219
} else {
208
220
if (loadNotApng) {
209
221
context.runOnUiThread {
@@ -226,10 +238,12 @@ class ApngAnimator(private val context: Context) {
226
238
activeAnimation = anim
227
239
uiThread {
228
240
imageView?.apply {
241
+ scaleType = this @ApngAnimator.scaleType ? : ImageView .ScaleType .FIT_CENTER
229
242
setImageBitmap(generatedFrame[0 ])
230
243
setImageDrawable(activeAnimation)
231
244
}
232
245
activeAnimation?.start()
246
+ isPlaying = true
233
247
doOnLoaded(this @ApngAnimator)
234
248
}
235
249
}
@@ -343,6 +357,20 @@ class ApngAnimator(private val context: Context) {
343
357
doOnLoaded = f
344
358
}
345
359
360
+ fun centerCrop (boolean : Boolean ) {
361
+ if (isApng) {
362
+ if (boolean) {
363
+ imageView?.scaleType = ImageView .ScaleType .CENTER_CROP
364
+ pause()
365
+ play()
366
+ } else {
367
+ imageView?.scaleType = null
368
+ pause()
369
+ play()
370
+ }
371
+ }
372
+ }
373
+
346
374
/* *
347
375
* Converts the generated frames into an animation drawable ([CustomAnimationDrawable])
348
376
* in the APNG will be used instead.
0 commit comments