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

Commit

Permalink
Merge pull request #305 from svga/2.5.11_hotfix
Browse files Browse the repository at this point in the history
bug 修复
  • Loading branch information
PonyCui authored Sep 23, 2020
2 parents a2ce648 + 4e3cc8e commit e53b0e3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class AnimationWithDynamicImageActivity extends Activity {

SVGAImageView animationView = null;
SVGAParser parser = new SVGAParser(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -32,6 +31,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

private void loadAnimation() {
try { // new URL needs try catch.
SVGAParser parser = new SVGAParser(this);
parser.decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/kingset.svga?raw=true"), new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class SVGAParser(context: Context?) {
return
}
try {
val cacheDir = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")
val cacheDir = SVGACache.buildCacheDir(cacheKey)
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
try {
LogUtils.info(TAG, "binary change to entity")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.graphics.Matrix
import android.graphics.Path
import android.graphics.RectF
import com.opensource.svgaplayer.proto.ShapeEntity
import org.json.JSONArray
import org.json.JSONObject
import java.util.HashMap
import java.util.*

/**
* Created by cuiminghui on 2017/2/22.
Expand Down Expand Up @@ -139,12 +140,24 @@ internal class SVGAVideoShapeEntity {
val styles = Styles()
it.optJSONArray("fill")?.let {
if (it.length() == 4) {
styles.fill = Color.argb((it.optDouble(3) * 255).toInt(), (it.optDouble(0) * 255).toInt(), (it.optDouble(1) * 255).toInt(), (it.optDouble(2) * 255).toInt())
val mulValue = checkValueRange(it)
styles.fill = Color.argb(
(it.optDouble(3) * mulValue).toInt(),
(it.optDouble(0) * mulValue).toInt(),
(it.optDouble(1) * mulValue).toInt(),
(it.optDouble(2) * mulValue).toInt()
)
}
}
it.optJSONArray("stroke")?.let {
if (it.length() == 4) {
styles.stroke = Color.argb((it.optDouble(3) * 255).toInt(), (it.optDouble(0) * 255).toInt(), (it.optDouble(1) * 255).toInt(), (it.optDouble(2) * 255).toInt())
val mulValue = checkValueRange(it)
styles.stroke = Color.argb(
(it.optDouble(3) * mulValue).toInt(),
(it.optDouble(0) * mulValue).toInt(),
(it.optDouble(1) * mulValue).toInt(),
(it.optDouble(2) * mulValue).toInt()
)
}
}
styles.strokeWidth = it.optDouble("strokeWidth", 0.0).toFloat()
Expand All @@ -161,14 +174,41 @@ internal class SVGAVideoShapeEntity {
}
}

// 检查色域范围是否是 0-1
private fun checkValueRange(obj: JSONArray): Float {
return if (
obj.optDouble(3) <= 1 &&
obj.optDouble(0) <= 1 &&
obj.optDouble(1) <= 1 &&
obj.optDouble(2) <= 1
) {
255f
} else {
1f
}
}

private fun parseStyles(obj: ShapeEntity) {
obj.styles?.let {
val styles = Styles()
it.fill?.let {
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())
val mulValue = checkValueRange(it)
styles.fill = Color.argb(
((it.a ?: 0f) * mulValue).toInt(),
((it.r ?: 0f) * mulValue).toInt(),
((it.g ?: 0f) * mulValue).toInt(),
((it.b ?: 0f) * mulValue).toInt()
)
}
it.stroke?.let {
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())
val mulValue = checkValueRange(it)
styles.stroke = Color.argb(
((it.a ?: 0f) * mulValue).toInt(),
((it.r ?: 0f) * mulValue).toInt(),
((it.g ?: 0f) * mulValue).toInt(),
((it.b ?: 0f) * mulValue).toInt()
)

}
styles.strokeWidth = it.strokeWidth ?: 0.0f
it.lineCap?.let {
Expand All @@ -194,6 +234,20 @@ internal class SVGAVideoShapeEntity {
}
}

// 检查色域范围是否是 0-1
private fun checkValueRange(color: ShapeEntity.ShapeStyle.RGBAColor): Float {
return if (
(color.a ?: 0f) <= 1 &&
(color.r ?: 0f) <= 1 &&
(color.g ?: 0f) <= 1 &&
(color.b ?: 0f) <= 1
) {
255f
} else {
1f
}
}

private fun parseTransform(obj: JSONObject) {
obj.optJSONObject("transform")?.let {
val transform = Matrix()
Expand Down Expand Up @@ -252,8 +306,7 @@ internal class SVGAVideoShapeEntity {
(this.args?.get("d") as? String)?.let {
SVGAPathEntity(it).buildPath(sharedPath)
}
}
else if (this.type == Type.ellipse) {
} else if (this.type == Type.ellipse) {
val xv = this.args?.get("x") as? Number ?: return
val yv = this.args?.get("y") as? Number ?: return
val rxv = this.args?.get("radiusX") as? Number ?: return
Expand All @@ -263,8 +316,7 @@ internal class SVGAVideoShapeEntity {
val rx = rxv.toFloat()
val ry = ryv.toFloat()
sharedPath.addOval(RectF(x - rx, y - ry, x + rx, y + ry), Path.Direction.CW)
}
else if (this.type == Type.rect) {
} else if (this.type == Type.rect) {
val xv = this.args?.get("x") as? Number ?: return
val yv = this.args?.get("y") as? Number ?: return
val wv = this.args?.get("width") as? Number ?: return
Expand Down

0 comments on commit e53b0e3

Please sign in to comment.