Skip to content

Instantly share code, notes, and snippets.

@trinnguyen
Created March 22, 2023 10:18
Show Gist options
  • Save trinnguyen/b417eef3e3587dcf7b09242728d69c18 to your computer and use it in GitHub Desktop.
Save trinnguyen/b417eef3e3587dcf7b09242728d69c18 to your computer and use it in GitHub Desktop.
Draw rect drawable to overlay, best for testing Camera Preview
class RectDrawable(rect: Rect): Drawable() {
private val boundingRectPaint = Paint().also {
it.style = Paint.Style.STROKE
it.color = Color.RED
it.strokeWidth = 4f
it.alpha = 255
}
init {
bounds = rect
}
override fun draw(canvas: Canvas) {
canvas.drawRect(bounds, boundingRectPaint)
}
override fun setAlpha(alpha: Int) {
boundingRectPaint.alpha = alpha
}
override fun setColorFilter(colorFilter: ColorFilter?) {
boundingRectPaint.colorFilter = colorFilter
}
@Deprecated("Deprecated in Java")
override fun getOpacity(): Int {
return PixelFormat.OPAQUE
}
companion object {
fun ViewGroupOverlay.reloadCodes(items: List<Rect>) {
this.clear()
items.forEach {
this.add(RectDrawable(it))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment