Skip to content

Instantly share code, notes, and snippets.

@m4kvn
Created December 3, 2023 16:21
Show Gist options
  • Save m4kvn/d6e304c86e52658920d4a94b33303740 to your computer and use it in GitHub Desktop.
Save m4kvn/d6e304c86e52658920d4a94b33303740 to your computer and use it in GitHub Desktop.
@Composable
fun CompareViewer() {
val density = LocalDensity.current
val separatorWidth: Dp = 1.dp
val separatorWidthPx = with(density) { separatorWidth.toPx() }
BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
val parentWidthDp = maxWidth
val parentWidthPx = with(density) { parentWidthDp.toPx() }
val initialOffsetX = parentWidthPx / 2
var offsetX by remember { mutableStateOf(initialOffsetX) }
val boxWidthDp = with(density) { offsetX.toDp() }
Box(
modifier = Modifier
.background(Color.White)
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState(onDelta = { delta ->
offsetX = (offsetX + delta)
.coerceIn(0f, parentWidthPx - separatorWidthPx)
})
)
) {
Image(
imageVector = Icons.Default.AccountBox,
contentDescription = null,
modifier = Modifier
.size(parentWidthDp)
)
Box(
modifier = Modifier
.width(boxWidthDp)
.height(parentWidthDp)
.background(Color.White)
.clipToBounds()
.wrapContentWidth(Alignment.Start, unbounded = true)
) {
Image(
imageVector = Icons.Default.AccountCircle,
contentDescription = null,
modifier = Modifier
.size(parentWidthDp)
)
}
Box(
modifier = Modifier
.offset { IntOffset(x = offsetX.roundToInt(), y = 0) }
.width(separatorWidth)
.height(parentWidthDp)
.background(Color.Black)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment