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

Commit 3a39ff6

Browse files
committed
feat: draw matte sprite with PorterDuffXfermode(PorterDuff.Mode.DST_IN).
1 parent 3ab61b7 commit 3a39ff6

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed
70.3 KB
Binary file not shown.
70.2 KB
Binary file not shown.

library/src/main/java/com/opensource/svgaplayer/drawer/SGVADrawer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ open internal class SGVADrawer(val videoItem: SVGAVideoEntity) {
1414

1515
val scaleInfo = SVGAScaleInfo()
1616

17-
inner class SVGADrawerSprite(val imageKey: String?, val frameEntity: SVGAVideoSpriteFrameEntity)
17+
inner class SVGADrawerSprite(val matteKey: String?, val imageKey: String?, val frameEntity: SVGAVideoSpriteFrameEntity)
1818

1919
internal fun requestFrameSprites(frameIndex: Int): List<SVGADrawerSprite> {
2020
return videoItem.sprites.mapNotNull {
2121
if (frameIndex >= 0 && frameIndex < it.frames.size) {
2222
if (it.frames[frameIndex].alpha <= 0.0) {
2323
return@mapNotNull null
2424
}
25-
return@mapNotNull SVGADrawerSprite(it.imageKey, it.frames[frameIndex])
25+
return@mapNotNull SVGADrawerSprite(it.matteKey, it.imageKey, it.frames[frameIndex])
2626
}
2727
return@mapNotNull null
2828
}

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,58 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
2121
super.drawFrame(canvas,frameIndex, scaleType)
2222
this.pathCache.onSizeChanged(canvas)
2323
val sprites = requestFrameSprites(frameIndex)
24-
sprites.forEach {
25-
drawSprite(it, canvas, frameIndex)
24+
val matteSprites = mutableMapOf<String, SVGADrawerSprite>()
25+
26+
var isMatteing = false
27+
28+
sprites.forEachIndexed { index, svgaDrawerSprite ->
29+
30+
// save matte sprite
31+
svgaDrawerSprite.imageKey?.let {
32+
if (it.endsWith(".matte")) {
33+
matteSprites.put(it, svgaDrawerSprite)
34+
// continue
35+
return@forEachIndexed
36+
}
37+
}
38+
39+
sprites.get(index - 1)?.let { lastSprite ->
40+
if (isMatteing && (svgaDrawerSprite.matteKey == null || svgaDrawerSprite.matteKey != lastSprite.matteKey)) {
41+
isMatteing = false
42+
43+
matteSprites.get(svgaDrawerSprite.matteKey)?.let {
44+
val matteBitmap = Bitmap.createBitmap(canvas.width, canvas.height, Bitmap.Config.ARGB_8888)
45+
val matteCanvas = Canvas(matteBitmap)
46+
val paint = Paint()
47+
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.DST_IN))
48+
49+
drawSprite(it, matteCanvas, frameIndex)
50+
canvas.drawBitmap(matteBitmap, 0f, 0f, paint)
51+
canvas.restore()
52+
}
53+
}
54+
if (svgaDrawerSprite.matteKey != null && (lastSprite.matteKey == null || lastSprite.matteKey != svgaDrawerSprite.matteKey)) {
55+
isMatteing = true
56+
canvas.save()
57+
}
58+
}
59+
60+
drawSprite(svgaDrawerSprite, canvas, frameIndex)
61+
62+
// if current sprite is the last one and isMatteing
63+
if (isMatteing && index == sprites.count() - 1) {
64+
matteSprites.get(svgaDrawerSprite.matteKey)?.let {
65+
val matteBitmap = Bitmap.createBitmap(canvas.width, canvas.height, Bitmap.Config.ARGB_8888)
66+
val matteCanvas = Canvas(matteBitmap)
67+
val paint = Paint()
68+
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.DST_IN))
69+
70+
drawSprite(it, matteCanvas, frameIndex)
71+
canvas.drawBitmap(matteBitmap, 0f, 0f, paint)
72+
canvas.restore()
73+
}
74+
}
75+
2676
}
2777
playAudio(frameIndex)
2878
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import org.json.JSONObject
88
*/
99
internal class SVGAVideoSpriteEntity {
1010

11+
val matteKey: String?
12+
1113
val imageKey: String?
1214

1315
val frames: List<SVGAVideoSpriteFrameEntity>
1416

1517
constructor(obj: JSONObject) {
18+
this.matteKey = obj.optString("matteKey")
1619
this.imageKey = obj.optString("imageKey")
1720
val mutableFrames: MutableList<SVGAVideoSpriteFrameEntity> = mutableListOf()
1821
obj.optJSONArray("frames")?.let {
@@ -34,6 +37,7 @@ internal class SVGAVideoSpriteEntity {
3437
}
3538

3639
constructor(obj: SpriteEntity) {
40+
this.matteKey = obj.imageKey
3741
this.imageKey = obj.imageKey
3842
var lastFrame: SVGAVideoSpriteFrameEntity? = null
3943
frames = obj.frames?.map {

0 commit comments

Comments
 (0)