Skip to content

Commit f605c5b

Browse files
committed
fix: Add canvas save and restore when Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP.
1 parent 0db05f2 commit f605c5b

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

115 KB
Binary file not shown.

‎app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromAssetsActivity.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void onError() {
5353

5454
private String randomSample() {
5555
if (samples.size() == 0) {
56+
samples.add("gradientBorder.svga");
5657
samples.add("Goddess.svga");
5758
samples.add("Rocket.svga");
5859
samples.add("angel.svga");

‎library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
2929
this.pathCache.onSizeChanged(canvas)
3030
val sprites = requestFrameSprites(frameIndex)
3131
val matteSprites = mutableMapOf<String, SVGADrawerSprite>()
32-
var saveID = 0
32+
var saveID = -1
3333
beginIndexList = null
3434
endIndexList = null
3535

36-
// Filte no matte layer
36+
// Filter no matte layer
3737
var hasMatteLayer = false
3838
sprites.get(0).imageKey?.let {
3939
if (it.endsWith(".matte")) {
@@ -42,10 +42,10 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
4242
}
4343
sprites.forEachIndexed { index, svgaDrawerSprite ->
4444

45-
// save matte sprite
45+
// Save matte sprite
4646
svgaDrawerSprite.imageKey?.let {
47-
/// No matte layer included
48-
if (!hasMatteLayer) {
47+
/// No matte layer included or VERSION Unsopport matte
48+
if (!hasMatteLayer || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
4949
// Normal sprite
5050
drawSprite(svgaDrawerSprite, canvas, frameIndex)
5151
// Continue
@@ -62,6 +62,8 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
6262
if (isMatteBegin(index, sprites)) {
6363
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
6464
saveID = canvas.saveLayer(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat(), null)
65+
} else {
66+
canvas.save()
6567
}
6668
}
6769
/// Normal matte
@@ -72,7 +74,11 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
7274
matteSprites.get(svgaDrawerSprite.matteKey)?.let {
7375
drawSprite(it, this.sharedValues.shareMatteCanvas(canvas.width, canvas.height), frameIndex)
7476
canvas.drawBitmap(this.sharedValues.sharedMatteBitmap(), 0f, 0f, this.sharedValues.shareMattePaint())
75-
canvas.restoreToCount(saveID)
77+
if (saveID != -1) {
78+
canvas.restoreToCount(saveID)
79+
} else {
80+
canvas.restore()
81+
}
7682
// Continue
7783
return@forEachIndexed
7884
}

0 commit comments

Comments
 (0)