Skip to content

Commit 422cbf6

Browse files
XinyueZsamtstern
authored andcommitted
Optimize CloudLandmarkGraphic (#697)
1 parent 1cc6240 commit 422cbf6

File tree

1 file changed

+28
-33
lines changed
  • mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition

1 file changed

+28
-33
lines changed

mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/kotlin/cloudlandmarkrecognition/CloudLandmarkGraphic.kt

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ import com.google.firebase.samples.apps.mlkit.common.GraphicOverlay
1010
/** Graphic instance for rendering detected landmark. */
1111
class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(overlay) {
1212

13-
private val rectPaint: Paint = Paint()
14-
private val landmarkPaint: Paint
15-
private lateinit var landmark: FirebaseVisionCloudLandmark
16-
17-
init {
18-
19-
rectPaint.color = TEXT_COLOR
20-
rectPaint.style = Paint.Style.STROKE
21-
rectPaint.strokeWidth = STROKE_WIDTH
22-
23-
landmarkPaint = Paint()
24-
landmarkPaint.color = TEXT_COLOR
25-
landmarkPaint.textSize = TEXT_SIZE
13+
private val rectPaint = Paint().apply {
14+
color = TEXT_COLOR
15+
style = Paint.Style.STROKE
16+
strokeWidth = STROKE_WIDTH
2617
}
18+
private val landmarkPaint = Paint().apply {
19+
color = TEXT_COLOR
20+
textSize = TEXT_SIZE
21+
}
22+
private var landmark: FirebaseVisionCloudLandmark? = null
2723

2824
/**
2925
* Updates the landmark instance from the detection of the most recent frame. Invalidates the
@@ -38,30 +34,29 @@ class CloudLandmarkGraphic(overlay: GraphicOverlay) : GraphicOverlay.Graphic(ove
3834
* Draws the landmark block annotations for position, size, and raw value on the supplied canvas.
3935
*/
4036
override fun draw(canvas: Canvas) {
41-
if (landmark == null) {
42-
throw IllegalStateException("Attempting to draw a null landmark.")
43-
}
44-
if (landmark.landmark == null || landmark.boundingBox == null) {
45-
return
46-
}
47-
48-
// Draws the bounding box around the LandmarkBlock.
49-
val rect = RectF(landmark.boundingBox)
50-
with(rect) {
51-
left = translateX(left)
52-
top = translateY(top)
53-
right = translateX(right)
54-
bottom = translateY(bottom)
55-
canvas.drawRect(this, rectPaint)
56-
57-
// Renders the landmark at the bottom of the box.
58-
canvas.drawText(landmark.landmark, left, bottom, landmarkPaint)
59-
}
37+
landmark?.let { lm ->
38+
if (lm.landmark == null || lm.boundingBox == null) {
39+
return
40+
}
41+
42+
// Draws the bounding box around the LandmarkBlock.
43+
val rect = RectF(lm.boundingBox)
44+
with(rect) {
45+
left = translateX(left)
46+
top = translateY(top)
47+
right = translateX(right)
48+
bottom = translateY(bottom)
49+
canvas.drawRect(this, rectPaint)
50+
51+
// Renders the landmark at the bottom of the box.
52+
canvas.drawText(lm.landmark, left, bottom, landmarkPaint)
53+
}
54+
} ?: kotlin.run { throw IllegalStateException("Attempting to draw a null landmark.") }
6055
}
6156

6257
companion object {
6358
private const val TEXT_COLOR = Color.WHITE
6459
private const val TEXT_SIZE = 54.0f
6560
private const val STROKE_WIDTH = 4.0f
6661
}
67-
}
62+
}

0 commit comments

Comments
 (0)