Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit e53b0e3

Browse files
authored
Merge pull request #305 from svga/2.5.11_hotfix
bug 修复
2 parents a2ce648 + 4e3cc8e commit e53b0e3

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationWithDynamicImageActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
public class AnimationWithDynamicImageActivity extends Activity {
2020

2121
SVGAImageView animationView = null;
22-
SVGAParser parser = new SVGAParser(this);
2322

2423
@Override
2524
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -32,6 +31,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3231

3332
private void loadAnimation() {
3433
try { // new URL needs try catch.
34+
SVGAParser parser = new SVGAParser(this);
3535
parser.decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/kingset.svga?raw=true"), new SVGAParser.ParseCompletion() {
3636
@Override
3737
public void onComplete(@NotNull SVGAVideoEntity videoItem) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class SVGAParser(context: Context?) {
380380
return
381381
}
382382
try {
383-
val cacheDir = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")
383+
val cacheDir = SVGACache.buildCacheDir(cacheKey)
384384
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
385385
try {
386386
LogUtils.info(TAG, "binary change to entity")

library/src/main/java/com/opensource/svgaplayer/entities/SVGAVideoShapeEntity.kt

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import android.graphics.Matrix
55
import android.graphics.Path
66
import android.graphics.RectF
77
import com.opensource.svgaplayer.proto.ShapeEntity
8+
import org.json.JSONArray
89
import org.json.JSONObject
9-
import java.util.HashMap
10+
import java.util.*
1011

1112
/**
1213
* Created by cuiminghui on 2017/2/22.
@@ -139,12 +140,24 @@ internal class SVGAVideoShapeEntity {
139140
val styles = Styles()
140141
it.optJSONArray("fill")?.let {
141142
if (it.length() == 4) {
142-
styles.fill = Color.argb((it.optDouble(3) * 255).toInt(), (it.optDouble(0) * 255).toInt(), (it.optDouble(1) * 255).toInt(), (it.optDouble(2) * 255).toInt())
143+
val mulValue = checkValueRange(it)
144+
styles.fill = Color.argb(
145+
(it.optDouble(3) * mulValue).toInt(),
146+
(it.optDouble(0) * mulValue).toInt(),
147+
(it.optDouble(1) * mulValue).toInt(),
148+
(it.optDouble(2) * mulValue).toInt()
149+
)
143150
}
144151
}
145152
it.optJSONArray("stroke")?.let {
146153
if (it.length() == 4) {
147-
styles.stroke = Color.argb((it.optDouble(3) * 255).toInt(), (it.optDouble(0) * 255).toInt(), (it.optDouble(1) * 255).toInt(), (it.optDouble(2) * 255).toInt())
154+
val mulValue = checkValueRange(it)
155+
styles.stroke = Color.argb(
156+
(it.optDouble(3) * mulValue).toInt(),
157+
(it.optDouble(0) * mulValue).toInt(),
158+
(it.optDouble(1) * mulValue).toInt(),
159+
(it.optDouble(2) * mulValue).toInt()
160+
)
148161
}
149162
}
150163
styles.strokeWidth = it.optDouble("strokeWidth", 0.0).toFloat()
@@ -161,14 +174,41 @@ internal class SVGAVideoShapeEntity {
161174
}
162175
}
163176

177+
// 检查色域范围是否是 0-1
178+
private fun checkValueRange(obj: JSONArray): Float {
179+
return if (
180+
obj.optDouble(3) <= 1 &&
181+
obj.optDouble(0) <= 1 &&
182+
obj.optDouble(1) <= 1 &&
183+
obj.optDouble(2) <= 1
184+
) {
185+
255f
186+
} else {
187+
1f
188+
}
189+
}
190+
164191
private fun parseStyles(obj: ShapeEntity) {
165192
obj.styles?.let {
166193
val styles = Styles()
167194
it.fill?.let {
168-
styles.fill = Color.argb(((it.a ?: 0.0f) * 255).toInt(), ((it.r ?: 0.0f) * 255).toInt(), ((it.g ?: 0.0f) * 255).toInt(), ((it.b ?: 0.0f) * 255).toInt())
195+
val mulValue = checkValueRange(it)
196+
styles.fill = Color.argb(
197+
((it.a ?: 0f) * mulValue).toInt(),
198+
((it.r ?: 0f) * mulValue).toInt(),
199+
((it.g ?: 0f) * mulValue).toInt(),
200+
((it.b ?: 0f) * mulValue).toInt()
201+
)
169202
}
170203
it.stroke?.let {
171-
styles.stroke = Color.argb(((it.a ?: 0.0f) * 255).toInt(), ((it.r ?: 0.0f) * 255).toInt(), ((it.g ?: 0.0f) * 255).toInt(), ((it.b ?: 0.0f) * 255).toInt())
204+
val mulValue = checkValueRange(it)
205+
styles.stroke = Color.argb(
206+
((it.a ?: 0f) * mulValue).toInt(),
207+
((it.r ?: 0f) * mulValue).toInt(),
208+
((it.g ?: 0f) * mulValue).toInt(),
209+
((it.b ?: 0f) * mulValue).toInt()
210+
)
211+
172212
}
173213
styles.strokeWidth = it.strokeWidth ?: 0.0f
174214
it.lineCap?.let {
@@ -194,6 +234,20 @@ internal class SVGAVideoShapeEntity {
194234
}
195235
}
196236

237+
// 检查色域范围是否是 0-1
238+
private fun checkValueRange(color: ShapeEntity.ShapeStyle.RGBAColor): Float {
239+
return if (
240+
(color.a ?: 0f) <= 1 &&
241+
(color.r ?: 0f) <= 1 &&
242+
(color.g ?: 0f) <= 1 &&
243+
(color.b ?: 0f) <= 1
244+
) {
245+
255f
246+
} else {
247+
1f
248+
}
249+
}
250+
197251
private fun parseTransform(obj: JSONObject) {
198252
obj.optJSONObject("transform")?.let {
199253
val transform = Matrix()
@@ -252,8 +306,7 @@ internal class SVGAVideoShapeEntity {
252306
(this.args?.get("d") as? String)?.let {
253307
SVGAPathEntity(it).buildPath(sharedPath)
254308
}
255-
}
256-
else if (this.type == Type.ellipse) {
309+
} else if (this.type == Type.ellipse) {
257310
val xv = this.args?.get("x") as? Number ?: return
258311
val yv = this.args?.get("y") as? Number ?: return
259312
val rxv = this.args?.get("radiusX") as? Number ?: return
@@ -263,8 +316,7 @@ internal class SVGAVideoShapeEntity {
263316
val rx = rxv.toFloat()
264317
val ry = ryv.toFloat()
265318
sharedPath.addOval(RectF(x - rx, y - ry, x + rx, y + ry), Path.Direction.CW)
266-
}
267-
else if (this.type == Type.rect) {
319+
} else if (this.type == Type.rect) {
268320
val xv = this.args?.get("x") as? Number ?: return
269321
val yv = this.args?.get("y") as? Number ?: return
270322
val wv = this.args?.get("width") as? Number ?: return

0 commit comments

Comments
 (0)