How do I make a canvas to write text on, draw imag...
# compose
s
How do I make a canvas to write text on, draw images like stickers, etc.? I'm trying to make something like a photo editor. Should I use
ImageBitmap
? If yes, how do I draw text on the bitmap since there's no
drawText
for
Canvas(bitmap)
d
I used TextDelegate to draw text on canvas here https://kotlinlang.slack.com/archives/CJLTWPH7S/p1612899162076200
s
Okay I'll try that
@Nader Jawad I see you've suggested
ImageBitmap
for this kind of stuff but I have a problem. Let's say I draw a text to my bitmap but later I want to delete the text (or maybe move the text by dragging), how can I do that? Do I need to make a new bitmap and canvas and redraw my texts and images every time I delete them or change their position? I thought of something like this but I'm not sure if it's efficient:
Copy code
var mainBitmap = mutableStateOf(ImageBitmap(size.width, size.height))
private var canvas = Canvas(mainBitmap.value)

fun addText(text: String) {
    texts.add(text)
    redrawCanvas()
}

fun onTextPositionChange(newPosition: Offset) {
    ...
    redrawCanvas()
}

fun redrawCanvas() {
    mainBitmap.value = ImageBitmap(size.width, size.height)
    canvas = Canvas(mainBitmap.value)

    texts.forEach {
        canvas.nativeCanvas.drawText(...)
    }
}