Skip to content

Commit bdb4d23

Browse files
committed
feat(android): initial svg support
1 parent 9203636 commit bdb4d23

File tree

2 files changed

+191
-47
lines changed
  • packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas

2 files changed

+191
-47
lines changed

packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas/SVGView.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.github.triniwiz.canvas
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.graphics.Canvas
6+
import android.graphics.Color
7+
import android.util.AttributeSet
8+
import android.util.Log
9+
import android.view.View
10+
import java.util.concurrent.Executors
11+
12+
/**
13+
* Created by triniwiz on 2019-08-05
14+
*/
15+
class TNSSVG : View {
16+
internal var bitmap: Bitmap? = null
17+
private var svgCanvas: Long = 0
18+
private var isInit = false
19+
internal val lock = Any()
20+
private var pendingInvalidate: Boolean = false
21+
private val executor = Executors.newSingleThreadExecutor()
22+
private var src: String = ""
23+
private var srcPath: String = ""
24+
25+
constructor(context: Context) : super(context, null) {
26+
init(context)
27+
}
28+
29+
constructor(context: Context, useCpu: Boolean) : super(context, null) {
30+
init(context)
31+
}
32+
33+
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
34+
init(context)
35+
}
36+
37+
private fun init(context: Context) {
38+
if (isInEditMode) {
39+
return
40+
}
41+
if (!TNSCanvas.isLibraryLoaded) {
42+
System.loadLibrary("canvasnative")
43+
TNSCanvas.isLibraryLoaded = true
44+
}
45+
}
46+
47+
48+
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
49+
super.onSizeChanged(w, h, oldw, oldh)
50+
if (w != 0 && h != 0) {
51+
val metrics = resources.displayMetrics
52+
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
53+
Log.d("com.test", "bm ${Canvas(bitmap!!).maximumBitmapWidth} x ${Canvas(bitmap!!).maximumBitmapHeight}")
54+
if (svgCanvas == 0L) {
55+
executor.execute {
56+
synchronized(lock) {
57+
svgCanvas = TNSCanvas.nativeInitContextWithCustomSurface(
58+
w.toFloat(),
59+
h.toFloat(),
60+
metrics.density,
61+
true,
62+
Color.BLACK,
63+
metrics.densityDpi.toFloat(),
64+
TNSCanvas.direction.toNative()
65+
)
66+
67+
if (srcPath.isNotEmpty() || src.isNotEmpty()) {
68+
69+
if (srcPath.isNotEmpty()) {
70+
nativeDrawSVGFromPath(svgCanvas, srcPath)
71+
} else {
72+
nativeDrawSVG(svgCanvas, src)
73+
}
74+
bitmap?.let {
75+
TNSCanvas.nativeCustomWithBitmapFlush(svgCanvas, it)
76+
handler!!.post {
77+
pendingInvalidate = false
78+
invalidate()
79+
}
80+
}
81+
82+
}
83+
}
84+
}
85+
} else {
86+
executor.execute {
87+
val metrics = resources.displayMetrics
88+
TNSCanvas.nativeResizeCustomSurface(
89+
svgCanvas,
90+
w.toFloat(),
91+
h.toFloat(),
92+
metrics.density,
93+
true,
94+
metrics.densityDpi,
95+
)
96+
97+
if (srcPath.isNotEmpty() || src.isNotEmpty()) {
98+
if (srcPath.isNotEmpty()) {
99+
nativeDrawSVGFromPath(svgCanvas, srcPath)
100+
} else {
101+
nativeDrawSVG(svgCanvas, src)
102+
}
103+
bitmap?.let {
104+
TNSCanvas.nativeCustomWithBitmapFlush(svgCanvas, it)
105+
handler!!.post {
106+
pendingInvalidate = false
107+
invalidate()
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
override fun onDraw(canvas: Canvas) {
117+
bitmap?.let {
118+
canvas.drawBitmap(it, 0f, 0f, null)
119+
}
120+
}
121+
122+
fun flush() {
123+
bitmap?.let {
124+
synchronized(lock) {
125+
if (svgCanvas != 0L) {
126+
executor.execute {
127+
TNSCanvas.nativeCustomWithBitmapFlush(svgCanvas, it)
128+
handler!!.post {
129+
pendingInvalidate = false
130+
invalidate()
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
138+
private fun doDraw() {
139+
synchronized(lock) {
140+
if (svgCanvas != 0L) {
141+
executor.execute {
142+
if (srcPath.isNotEmpty()) {
143+
nativeDrawSVGFromPath(svgCanvas, srcPath)
144+
bitmap?.let {
145+
if (svgCanvas != 0L) {
146+
executor.execute {
147+
TNSCanvas.nativeCustomWithBitmapFlush(svgCanvas, it)
148+
handler!!.post {
149+
pendingInvalidate = false
150+
invalidate()
151+
}
152+
}
153+
}
154+
}
155+
} else {
156+
nativeDrawSVG(svgCanvas, src)
157+
bitmap?.let {
158+
if (svgCanvas != 0L) {
159+
executor.execute {
160+
TNSCanvas.nativeCustomWithBitmapFlush(svgCanvas, it)
161+
handler!!.post {
162+
pendingInvalidate = false
163+
invalidate()
164+
}
165+
}
166+
}
167+
}
168+
}
169+
}
170+
}
171+
}
172+
}
173+
174+
fun setSrc(src: String) {
175+
this.src = src
176+
doDraw()
177+
}
178+
179+
fun setSrcPath(path: String) {
180+
this.srcPath = path
181+
doDraw()
182+
}
183+
184+
companion object {
185+
@JvmStatic
186+
private external fun nativeDrawSVG(svgCanvas: Long, svg: String)
187+
188+
@JvmStatic
189+
private external fun nativeDrawSVGFromPath(svgCanvas: Long, svg: String)
190+
}
191+
}

0 commit comments

Comments
 (0)