Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose offset image vector in Canvas

I have a problem with vector image in Canvas. As shown below I can just call vector image but I can’t make any offset in Canvas. So I only can have it the way it is.

I don't know the reason why there is no Offset option like in drawCircle or drawRect, if someone has some ideas it would be great.

val vector = ImageVector.vectorResource(id = R.drawable.ic_test)
val painter = rememberVectorPainter(image = vector)

Box(contentAlignment = Alignment.Center) {
    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp)
    ) {
            with(painter) {
                draw(
                    painter.intrinsicSize
                )
            }

        }
    }

I tried something like adding Offset into with(painter) but nothing changes:

with(painter) {
    draw(
        painter.intrinsicSize
    )
    Offset(x = 10f, y = 10f)
}
like image 309
Nikola C Avatar asked Feb 22 '26 05:02

Nikola C


1 Answers

You can use DrawScope.translate:

translate(left = 10f, top = 10f) {
    with(painter) {
        draw(
            painter.intrinsicSize
        )
    }
}
like image 183
Philip Dukhov Avatar answered Feb 25 '26 04:02

Philip Dukhov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!