@@ -26,13 +26,11 @@ actual open class QRCodeGraphics actual constructor(
26
26
val AVAILABLE_FORMATS : Array <String > = CompressFormat .entries.map { it.name }.toTypedArray()
27
27
}
28
28
29
- protected fun createCanvas (image : Bitmap ) = Canvas (image)
30
-
31
29
/* * [Bitmap] being used for the drawing operations. */
32
30
private var image: Bitmap = Bitmap .createBitmap(width, height, ARGB_8888 )
33
31
34
32
/* * [Canvas] that handles the presenting. */
35
- private var canvas: Canvas = createCanvas(image)
33
+ private lateinit var canvas: Canvas
36
34
37
35
/* * Cache of [Paint] objects being used. Just to try and use the least amount of CPU/memory possible. */
38
36
private val paintCache = mutableMapOf<Int , Paint >()
@@ -45,6 +43,14 @@ actual open class QRCodeGraphics actual constructor(
45
43
private var customCanvasOffsetX: Int = 0
46
44
private var customCanvasOffsetY: Int = 0
47
45
46
+ protected fun useCanvas (): Canvas {
47
+ if (! customCanvas && ! this ::canvas.isInitialized) {
48
+ canvas = Canvas (image)
49
+ }
50
+
51
+ return canvas
52
+ }
53
+
48
54
/* *
49
55
* Allows this object to draw directly to a user specified [Canvas].
50
56
*
@@ -93,12 +99,7 @@ actual open class QRCodeGraphics actual constructor(
93
99
if (changed) {
94
100
changed = false
95
101
image = Bitmap .createBitmap(width, height, ARGB_8888 )
96
-
97
- if (! customCanvas) {
98
- canvas = createCanvas(image)
99
- } else {
100
- this .canvas.setBitmap(image)
101
- }
102
+ useCanvas().setBitmap(image)
102
103
}
103
104
}
104
105
@@ -163,7 +164,7 @@ actual open class QRCodeGraphics actual constructor(
163
164
164
165
/* * Draw a straight line from point `(x1,y1)` to `(x2,y2)`. */
165
166
actual open fun drawLine (x1 : Int , y1 : Int , x2 : Int , y2 : Int , color : Int , thickness : Double ) {
166
- canvas .drawLine(
167
+ useCanvas() .drawLine(
167
168
(x1 + customCanvasOffsetX).toFloat(),
168
169
(y1 + customCanvasOffsetY).toFloat(),
169
170
(x2 + customCanvasOffsetX).toFloat(),
@@ -181,12 +182,12 @@ actual open class QRCodeGraphics actual constructor(
181
182
x + width - halfThickness + customCanvasOffsetX,
182
183
y + height - halfThickness + customCanvasOffsetY,
183
184
)
184
- canvas .drawRect(rect, paintFromCache(color, STROKE , thickness))
185
+ useCanvas() .drawRect(rect, paintFromCache(color, STROKE , thickness))
185
186
}
186
187
187
188
/* * Fills the rectangle starting at point `(x,y)` and having `width` by `height`. */
188
189
actual open fun fillRect (x : Int , y : Int , width : Int , height : Int , color : Int ) {
189
- canvas .drawRect(
190
+ useCanvas() .drawRect(
190
191
Rect (
191
192
x + customCanvasOffsetX,
192
193
y + customCanvasOffsetY,
@@ -234,7 +235,7 @@ actual open class QRCodeGraphics actual constructor(
234
235
) {
235
236
val halfThickness = (thickness / 2.0 ).roundToInt()
236
237
237
- canvas .drawRoundRect(
238
+ useCanvas() .drawRoundRect(
238
239
buildRectF(x + halfThickness, y + halfThickness, width - halfThickness * 2 , height - halfThickness * 2 ),
239
240
borderRadius.toFloat(),
240
241
borderRadius.toFloat(),
@@ -264,7 +265,7 @@ actual open class QRCodeGraphics actual constructor(
264
265
*
265
266
*/
266
267
actual open fun fillRoundRect (x : Int , y : Int , width : Int , height : Int , borderRadius : Int , color : Int ) {
267
- canvas .drawRoundRect(
268
+ useCanvas() .drawRoundRect(
268
269
buildRectF(x, y, width, height),
269
270
borderRadius.toFloat(),
270
271
borderRadius.toFloat(),
@@ -276,7 +277,7 @@ actual open class QRCodeGraphics actual constructor(
276
277
* Draw the edges of an ellipse (aka "a circle") which occupies the area `(x,y,width,height)`
277
278
*/
278
279
actual fun drawEllipse (x : Int , y : Int , width : Int , height : Int , color : Int , thickness : Double ) {
279
- canvas .drawOval(
280
+ useCanvas() .drawOval(
280
281
buildRectF(x, y, width, height),
281
282
paintFromCache(color, STROKE , thickness),
282
283
)
@@ -287,7 +288,7 @@ actual open class QRCodeGraphics actual constructor(
287
288
*
288
289
*/
289
290
actual fun fillEllipse (x : Int , y : Int , width : Int , height : Int , color : Int ) {
290
- canvas .drawOval(
291
+ useCanvas() .drawOval(
291
292
buildRectF(x, y, width, height),
292
293
paintFromCache(color),
293
294
)
@@ -308,6 +309,6 @@ actual open class QRCodeGraphics actual constructor(
308
309
*/
309
310
open fun drawImage (img : Bitmap , x : Int , y : Int ) {
310
311
changed = true
311
- canvas .drawBitmap(img, x.toFloat(), y.toFloat(), null )
312
+ useCanvas() .drawBitmap(img, x.toFloat(), y.toFloat(), null )
312
313
}
313
314
}
0 commit comments