Misha Usov
Misha Usov

Reputation: 1

How to save graphic layer to bitmap

Here is a snippet of code, where i try to save prev image screenImageBitmap with new paths on the canvas with this code

val bitmap = graphicsLayer.toImageBitmap().asAndroidBitmap()

Everything is working without drawImage(it) in canvas part, i don understand why, can you explain why

graphicsLayer

Do not want to save full image with drawImage and Paths

@Composable
internal fun DrawPointerMotionEvents(
    paths: List<PathObject>,
    activePencil: Pencil,
    needSaveToFile: Boolean,
    accept: (event: DrawUiEvent) -> Unit,
    screenImageBitmap: ImageBitmap?,
) {
    var motionEvent by remember { mutableStateOf(MotionEvent.Idle) }
    var currentPosition by remember { mutableStateOf(Offset.Unspecified) }
    var previousPosition by remember { mutableStateOf(Offset.Unspecified) }
    val path = paths.last().path
    val density = LocalDensity.current
    val imageBitmap = remember(screenImageBitmap) { mutableStateOf(screenImageBitmap) }
    val drawModifier = Modifier
        .fillMaxSize()
        .clipToBounds()
        .background(MaterialTheme.colorScheme.background)
        .pointerMotionEvents(
            onDown = { pointerInputChange: PointerInputChange ->
                accept(DrawUiEvent.OnPathFinished)
                currentPosition = pointerInputChange.position
                motionEvent = MotionEvent.Down
                pointerInputChange.consume()
            },
            onMove = { pointerInputChange: PointerInputChange ->
                currentPosition = pointerInputChange.position
                motionEvent = MotionEvent.Move
                pointerInputChange.consume()
            },
            onUp = { pointerInputChange: PointerInputChange ->
                motionEvent = MotionEvent.Up
                pointerInputChange.consume()
            },
            delayAfterDownInMillis = 25L
        )

    val coroutineScope = rememberCoroutineScope()
    val graphicsLayer = rememberGraphicsLayer()

    LaunchedEffect(key1 = needSaveToFile) {
        if (needSaveToFile) {
            coroutineScope.launch(Dispatchers.IO) {
                val bitmap = graphicsLayer.toImageBitmap().asAndroidBitmap()
                accept(DrawUiEvent.BitmapCreated(bitmap))
            }
        }
    }

    Canvas(
        modifier = drawModifier.drawWithContent {
            graphicsLayer.apply {
                compositingStrategy = CompositingStrategy.Offscreen
                record {
                    [email protected]()
                }
            }
            drawLayer(graphicsLayer)
        }
    ) {
        when (motionEvent) {
            MotionEvent.Down -> {
                path.moveTo(currentPosition.x, currentPosition.y)
                previousPosition = currentPosition
            }
            MotionEvent.Move -> {
                path.quadraticBezierTo(
                    previousPosition.x,
                    previousPosition.y,
                    (previousPosition.x + currentPosition.x) / 2,
                    (previousPosition.y + currentPosition.y) / 2

                )
                previousPosition = currentPosition
            }
            MotionEvent.Up -> {
                path.lineTo(currentPosition.x, currentPosition.y)
                currentPosition = Offset.Unspecified
                previousPosition = currentPosition
                motionEvent = MotionEvent.Idle
            }
            else -> Unit
        }
        imageBitmap.value?.let {
            drawImage(it)
        }
        paths.forEach {
            if (it.isVisible) {
                val blendMode = if (it.color == Color.Transparent) BlendMode.Clear else BlendMode.SrcOver
                drawPath(
                    path = it.path,
                    color = it.color,
                    blendMode = blendMode,
                    style = Stroke(
                        cap = StrokeCap.Round,
                        join = StrokeJoin.Round,
                        width = it.strokeWidth.dp.toPx(),
                    )
                )
            }
        }
    }
}

Upvotes: 0

Views: 14

Answers (0)

Related Questions