1
1
package com.opensource.svgaplayer
2
2
3
- import android.content.res.Resources
4
3
import android.graphics.Bitmap
5
4
import android.graphics.BitmapFactory
6
- import android.graphics.drawable.BitmapDrawable
7
- import android.util.Log
8
-
9
- import org.json.JSONArray
10
- import org.json.JSONException
5
+ import com.opensource.svgaplayer.proto.MovieEntity
11
6
import org.json.JSONObject
12
-
13
7
import java.io.File
14
- import java.io.Serializable
15
- import java.util.ArrayList
16
- import java.util.HashMap
8
+ import java.util.*
17
9
18
10
/* *
19
11
* Created by PonyCui_Home on 16/6/18.
@@ -52,61 +44,57 @@ class SVGAVideoEntity {
52
44
resetSprites(obj)
53
45
}
54
46
55
- constructor (obj: Svga . MovieEntity , cacheDir: File ) {
47
+ constructor (obj: MovieEntity , cacheDir: File ) {
56
48
this .cacheDir = cacheDir
57
- if (obj.hasParams()) {
58
- obj.params?.let { movieParams ->
59
- videoSize = SVGARect (0.0 , 0.0 , movieParams.viewBoxWidth.toDouble(), movieParams.viewBoxHeight.toDouble())
60
- FPS = movieParams.fps
61
- frames = movieParams.frames
62
- }
49
+ obj.params?.let { movieParams ->
50
+ videoSize = SVGARect (0.0 , 0.0 , (movieParams.viewBoxWidth ? : 0.0f ).toDouble(), (movieParams.viewBoxHeight ? : 0.0f ).toDouble())
51
+ FPS = movieParams.fps ? : 20
52
+ frames = movieParams.frames ? : 0
63
53
}
64
54
resetImages(obj)
65
55
resetSprites(obj)
66
56
}
67
57
68
58
private fun resetImages (obj : JSONObject ) {
69
- obj.optJSONObject(" images" )?.let {
70
- val imgObjects = it
71
- it.keys().forEach {
72
- val imageKey = it
59
+ obj.optJSONObject(" images" )?.let { imgObjects ->
60
+ imgObjects.keys().forEach { imageKey ->
73
61
var filePath = cacheDir.absolutePath + " /" + imgObjects[imageKey]
74
62
var bitmap = if (File (filePath).exists()) BitmapFactory .decodeFile(filePath) else null
75
63
if (bitmap != null ) {
76
64
images.put(imageKey, bitmap)
77
65
}
78
66
else {
79
- filePath = cacheDir.absolutePath + " /" + imageKey + " .png"
80
- bitmap = if ( File (filePath).exists()) BitmapFactory .decodeFile(filePath) else null
81
- if (bitmap != null ) {
82
- images.put(imageKey, bitmap)
67
+ ( cacheDir.absolutePath + " /" + imageKey + " .png" )?. takeIf { File (it).exists() }?. let { it
68
+ BitmapFactory .decodeFile(it)?. let {
69
+ images.put(imageKey, it)
70
+ }
83
71
}
84
72
}
85
73
}
86
74
}
87
75
}
88
76
89
- private fun resetImages (obj : Svga . MovieEntity ) {
90
- obj.imagesMap .entries.forEach {
77
+ private fun resetImages (obj : MovieEntity ) {
78
+ obj.images? .entries? .forEach {
91
79
val imageKey = it.key
92
- if (it.value.isValidUtf8) {
93
- var filePath = cacheDir.absolutePath + " /" + it.value.toString()
94
- var bitmap = if (File (filePath).exists()) BitmapFactory .decodeFile(filePath) else null
95
- if (bitmap != null ) {
96
- images.put(imageKey, bitmap)
97
- }
98
- else {
99
- filePath = cacheDir.absolutePath + " /" + imageKey + " .png"
100
- bitmap = if (File (filePath).exists()) BitmapFactory .decodeFile(filePath) else null
80
+ val bitmap = BitmapFactory .decodeByteArray(it.value.toByteArray(), 0 , it.value.size())
81
+ if (bitmap != null ) {
82
+ images.put(imageKey, bitmap)
83
+ }
84
+ else {
85
+ it.value.utf8()?.let {
86
+ var filePath = cacheDir.absolutePath + " /" + it
87
+ var bitmap = if (File (filePath).exists()) BitmapFactory .decodeFile(filePath) else null
101
88
if (bitmap != null ) {
102
89
images.put(imageKey, bitmap)
103
90
}
104
- }
105
- }
106
- else {
107
- val bitmap = BitmapFactory .decodeByteArray(it.value.toByteArray(), 0 , it.value.size())
108
- if (bitmap != null ) {
109
- images.put(imageKey, bitmap)
91
+ else {
92
+ (cacheDir.absolutePath + " /" + imageKey + " .png" )?.takeIf { File (it).exists() }?.let { it
93
+ BitmapFactory .decodeFile(it)?.let {
94
+ images.put(imageKey, it)
95
+ }
96
+ }
97
+ }
110
98
}
111
99
}
112
100
}
@@ -124,10 +112,10 @@ class SVGAVideoEntity {
124
112
sprites = mutableList.toList()
125
113
}
126
114
127
- private fun resetSprites (obj : Svga . MovieEntity ) {
128
- sprites = obj.spritesList .map {
115
+ private fun resetSprites (obj : MovieEntity ) {
116
+ sprites = obj.sprites? .map {
129
117
return @map SVGAVideoSpriteEntity (it)
130
- }
118
+ } ? : listOf ()
131
119
}
132
120
133
121
}
0 commit comments