Skip to content

Commit c546705

Browse files
committed
improve: DONOT createPath until display it.
1 parent 10ae0f0 commit c546705

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
126126
canvas.clipRect(0, 0, drawingBitmap.width, drawingBitmap.height)
127127
val bitmapShader = BitmapShader(drawingBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
128128
sharedPaint.shader = bitmapShader
129-
canvas.drawPath(maskPath, sharedPaint)
129+
sharedPaint.isAntiAlias = true
130+
sharedPath.reset()
131+
maskPath.buildPath(sharedPath)
132+
canvas.drawPath(sharedPath, sharedPaint)
130133
canvas.restore()
131134
}
132135
else {
@@ -158,7 +161,10 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
158161
canvas.clipRect(0, 0, drawingBitmap.width, drawingBitmap.height)
159162
val bitmapShader = BitmapShader(textBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
160163
sharedPaint.shader = bitmapShader
161-
canvas.drawPath(maskPath, sharedPaint)
164+
sharedPaint.isAntiAlias = true
165+
sharedPath.reset()
166+
maskPath.buildPath(sharedPath)
167+
canvas.drawPath(sharedPath, sharedPaint)
162168
canvas.restore()
163169
}
164170
else {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.view.ViewPropertyAnimator
1414
import android.view.animation.LinearInterpolator
1515
import android.widget.ImageView
1616
import java.net.URL
17+
import java.util.*
1718

1819
/**
1920
* Created by cuiminghui on 2017/3/29.
@@ -127,8 +128,10 @@ open class SVGAImageView : ImageView {
127128
return@Thread
128129
}
129130
}
131+
System.out.println("ssss, s" + Date().time.toString())
130132
parser.parse(it, object : SVGAParser.ParseCompletion {
131133
override fun onComplete(videoItem: SVGAVideoEntity) {
134+
System.out.println("ssss, e" + Date().time.toString())
132135
handler?.post {
133136
videoItem.antiAlias = antiAlias
134137
setVideoItem(videoItem)

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package com.opensource.svgaplayer
33
import android.graphics.Path
44
import java.util.*
55

6-
internal class SVGAPath {
6+
class SVGAPath(val strValue: String) {
77

8+
var cachedPath: Path? = null
89
val VALID_METHODS: List<String> = listOf("M", "L", "H", "V", "C", "S", "Q", "R", "A", "Z", "m", "l", "h", "v", "c", "s", "q", "r", "a", "z")
910

10-
constructor(strValue: String, toPath: Path) {
11-
buildPath(strValue.split("[,\\s+]".toRegex()).dropLastWhile(String::isEmpty).toTypedArray(), toPath)
12-
}
13-
14-
private fun buildPath(items: Array<String>, toPath: Path) {
11+
fun buildPath(toPath: Path) {
12+
cachedPath?.let {
13+
toPath.addPath(it)
14+
return
15+
}
16+
val cachedPath = Path()
1517
var currentMethod = ""
1618
val args = ArrayList<SVGAPoint>()
1719
var argLast: String? = null
20+
val items = strValue.split("[,\\s+]".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
1821
for (item in items) {
1922
if (item.length < 1) {
2023
continue
@@ -24,7 +27,7 @@ internal class SVGAPath {
2427
argLast?.takeIf { it.isNotEmpty() }?.let {
2528
args.add(SVGAPoint(0.0f, 0.0f, try {it.toFloat()} catch (e: Exception) { 0.0f }))
2629
}
27-
this.operate(toPath, currentMethod, args)
30+
this.operate(cachedPath, currentMethod, args)
2831
args.clear()
2932
currentMethod = firstLetter
3033
argLast = item.substring(1)
@@ -41,7 +44,9 @@ internal class SVGAPath {
4144
}
4245
}
4346
}
44-
this.operate(toPath, currentMethod, args)
47+
this.operate(cachedPath, currentMethod, args)
48+
this.cachedPath = cachedPath
49+
toPath.addPath(cachedPath)
4550
}
4651

4752
private fun operate(finalPath: Path, method: String, args: List<SVGAPoint>) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class SVGAVideoShapeEntity(obj: JSONObject) {
157157
val aPath = Path()
158158
if (this.type == SVGAVideoShapeEntity.Type.shape) {
159159
(this.args?.get("d") as? String)?.let {
160-
SVGAPath(it, aPath)
160+
SVGAPath(it).buildPath(aPath)
161161
}
162162
}
163163
else if (this.type == SVGAVideoShapeEntity.Type.ellipse) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SVGAVideoSpriteFrameEntity(obj: JSONObject) {
1818
var alpha = obj.optDouble("alpha", 0.0)
1919
var layout = SVGARect(0.0, 0.0, 0.0, 0.0)
2020
var transform = Matrix()
21-
var maskPath: Path? = null
21+
var maskPath: SVGAPath? = null
2222
var shapes: List<SVGAVideoShapeEntity> = listOf()
2323

2424
init {
@@ -46,8 +46,7 @@ class SVGAVideoSpriteFrameEntity(obj: JSONObject) {
4646
}
4747
obj.optString("clipPath")?.let { d ->
4848
if (d.isNotEmpty()) {
49-
maskPath = Path()
50-
maskPath?.let { maskPath -> SVGAPath(d, maskPath) }
49+
maskPath = SVGAPath(d)
5150
}
5251
}
5352
obj.optJSONArray("shapes")?.let {

0 commit comments

Comments
 (0)