Updating image contents
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
An image that is already displayed can change: the user draws on it, a filter is applied, or a file is rewritten. This
page shows how to tell the view so that it draws the new contents. Positions in the examples are expressed in upright
image pixels (EXIF orientation already applied), the same ones used by mapViewToImage and by the overlay drawer.
Choosing the way
| Way | Memory | Cost of an update | When to use |
|---|---|---|---|
Mutable |
The whole bitmap, uncompressed, in memory. |
|
Small and medium images that are edited frequently. |
|
The whole bitmap in memory, plus the decoded levels within |
|
Large bitmaps drawn on by the caller, which stay smooth when zooming. |
File or other |
Only the preview, cached levels and patch. |
|
The image lives in a file that another component rewrites, or a custom source whose contents change. |
invalidateImage(region) takes the changed Rect in upright image pixels, or null for the whole image. Without an
image it does nothing.
A mutable bitmap
val bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
view.bitmap = bitmap
// edit the bitmap, then tell the view that its contents changed
Canvas(bitmap).drawColor(Color.RED)
view.invalidateImage()
A BitmapImageSource
The bitmap stays owned by the caller: the view never recycles it, not even when the source is closed. Reads are synchronized on the bitmap, so synchronize on it too while drawing.
// the bitmap stays owned by the caller: the view never recycles it
view.setImageSource(BitmapImageSource(bitmap))
// draw on the bitmap, synchronizing on it, and refresh only the edited region
val edited = Rect(100, 100, 300, 300) // upright image pixels
synchronized(bitmap) {
val paint = Paint().apply { color = Color.RED }
Canvas(bitmap).drawRect(edited, paint)
}
view.invalidateImage(edited)
A rewritten file
view.setImageFile(file)
// later, another component rewrites the file with an image of the same size
file.writeBytes(bytes)
// decode it again
view.invalidateImage()
The file must keep the dimensions of the image. To show an image of another size, set it again.
Complete draw mode
The view does not provide a drawing mode, but it provides the pieces to build one:
-
scrollEnabled,scaleEnabled,rotationEnabledanddoubleTapEnabledset tofalse, so gestures do not compete with drawing. -
An
OnTouchListenerthat converts the touches withmapViewToImage(x, y, result). It returnsfalsewhen there is no image or the display matrix cannot be inverted. -
An
ImageOverlayDrawerthat draws the stroke in progress as aPath. Its canvas is expressed in upright image pixels and clipped to the image, so the stroke is drawn with the same coordinates it was recorded in. -
When the stroke ends, it is drawn on the bitmap and
invalidateImage(region)refreshes only the region it touched.
// the image to draw on: the view displays it and the caller keeps ownership of the bitmap
view.setImageSource(BitmapImageSource(bitmap))
// gestures must not compete with drawing
view.scrollEnabled = false
view.scaleEnabled = false
view.rotationEnabled = false
view.doubleTapEnabled = false
val paint = Paint().apply {
color = Color.RED
style = Paint.Style.STROKE
strokeWidth = 8.0f
strokeCap = Paint.Cap.ROUND
strokeJoin = Paint.Join.ROUND
isAntiAlias = true
}
var stroke: Path? = null // stroke being drawn, in upright image pixels
val point = PointF()
// the stroke in progress is drawn above the image, in image pixels
view.imageOverlayDrawer = object : GestureBitmapView.ImageOverlayDrawer {
override fun onDrawOverImage(view: GestureBitmapView, canvas: Canvas) {
stroke?.let { canvas.drawPath(it, paint) }
}
}
view.setOnTouchListener { v, event ->
// convert the touch position from view pixels to image pixels
if (!view.mapViewToImage(event.x, event.y, point)) {
return@setOnTouchListener false
}
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
stroke = Path().apply { moveTo(point.x, point.y) }
}
MotionEvent.ACTION_MOVE -> {
stroke?.lineTo(point.x, point.y)
v.invalidate()
}
MotionEvent.ACTION_UP -> {
stroke?.let { finished ->
finished.lineTo(point.x, point.y)
// commit the stroke to the bitmap
synchronized(bitmap) {
Canvas(bitmap).drawPath(finished, paint)
}
// refresh only the region touched by the stroke
val bounds = RectF()
finished.computeBounds(bounds, true)
bounds.inset(-paint.strokeWidth, -paint.strokeWidth)
val region = Rect()
bounds.roundOut(region)
view.invalidateImage(region)
}
stroke = null
v.invalidate()
}
}
true
}