Skip to content

Commit b9e237d

Browse files
committed
Add a base class for visualization views
1 parent 21570a9 commit b9e237d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2022 JetBrains s.r.o. and Kotlin Deep Learning project contributors. All Rights Reserved.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlinx.dl.visualization
7+
8+
import android.content.Context
9+
import android.graphics.Canvas
10+
import android.util.AttributeSet
11+
import android.view.View
12+
13+
/**
14+
* Base class for [View] implementations which visualize detected results on top of the image preview.
15+
* Derived classes should implement [drawDetection] method to perform actual drawing.
16+
*/
17+
abstract class DetectorViewBase<T>(context: Context, attrs: AttributeSet) : View(context, attrs) {
18+
/**
19+
* Detection result to visualize
20+
*/
21+
var detection: T? = null
22+
set(value) {
23+
synchronized(this) {
24+
field = value
25+
postInvalidate()
26+
}
27+
}
28+
29+
/**
30+
* Draw given detection result on the [Canvas].
31+
*/
32+
abstract fun Canvas.drawDetection(detection: T)
33+
34+
override fun onDraw(canvas: Canvas) {
35+
super.onDraw(canvas)
36+
37+
synchronized(this) {
38+
val detection = detection
39+
if (detection != null) {
40+
canvas.drawDetection(detection)
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)