I am using clip to trim an Image using Compose, to show only the left edge part of the image. But it maintains the width with blank space. How can I crop the right part(marked in red)?
I tried setting a custom width for Image but its not working as expected.
Here is the code I am using,
object CropShape : Shape {
override fun createOutline(
size: androidx.compose.ui.geometry.Size,
layoutDirection: LayoutDirection,
density: Density
): Outline = Outline.Rectangle(
Rect(Offset.Zero, androidx.compose.ui.geometry.Size((58 * density.density), size.height))
)
}
@Composable
private fun test(){
Image(
modifier = Modifier
.height(142.dp)
.clip(CropShape)
.padding(start = 20.dp, bottom = 20.dp)
.rotate(TiltedImageRotation)
painter = resourcePainter(R.drawable.image),
contentScale = ContentScale.FillHeight,
contentDescription = null,
)
}
Setting the width is the right approach, you just need proper alignment
I think - use alignment = Alignment.CenterStart
to see the start of your image and not the center like on your second picture:
Image(
modifier = Modifier
.height(142.dp)
.width(58.dp)
.padding(start = 20.dp, bottom = 20.dp)
.rotate(TiltedImageRotation)
painter = resourcePainter(R.drawable.image),
contentScale = ContentScale.FillHeight,
alignment = Alignment.CenterStart,
contentDescription = null,
)
If you want to align with some offset, as suggested in the other answer, that can be done with Alignment
too, and more easily:
val density = LocalDensity.current
Image(
alignment = Alignment { _, _, _ ->
val xOffset = density.run { 58.dp.toPx() / 2 }.roundToInt()
IntOffset(x = -xOffset, 0)
}
)