Skip to content

Commit

Permalink
Rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy-chernysh committed Mar 21, 2024
1 parent f7d04a3 commit b3eca8d
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,8 @@ fun ImageViewerScreen() {
fun BoxScope.UpperImage() {
var imageScale by remember { mutableFloatStateOf(1f) }
var imageOffset by remember { mutableStateOf(Offset.Zero) }
var rotation by remember { mutableFloatStateOf(0f) }

val transformState = rememberTransformableState { zoomChange, panChange, rotationChange ->
imageScale = (imageScale * zoomChange).coerceIn(1f, 5f)
imageOffset += panChange
}

val model = ImageRequest.Builder(LocalContext.current)
.data("https://c4.wallpaperflare.com/wallpaper/83/500/871/waterfall-high-resolution-desktop-wallpaper-preview.jpg")
Expand All @@ -121,30 +118,34 @@ fun BoxScope.UpperImage() {
.diskCachePolicy(CachePolicy.DISABLED)
.build()

val transformState = rememberTransformableState { zoomChange, panChange, rotationChange ->
imageScale = (imageScale * zoomChange).coerceIn(0.5f, 5f)
imageOffset += panChange
rotation += rotationChange
}

AsyncImage(
model = model,
contentDescription = "",
contentScale = ContentScale.FillHeight,
onState = { state ->
Log.d("UI", "AsyncImage.state: $state")
},
modifier = Modifier
.align(Alignment.Center)
.fillMaxWidth()
.scale(imageScale)
.graphicsLayer {
Log.d("UI", "ImageViewerScreen: graphicsLayer: scale $scaleX $scaleY")
scaleX = imageScale
scaleY = imageScale
translationX = imageOffset.x
translationY = imageOffset.y
rotationZ = rotation
}
.transformable(transformState)
.pointerInput(Unit) {
detectTapGestures(onDoubleTap = {
Log.d("UI", "ImageViewerScreen: on double tap")
imageScale = 1f
imageOffset = Offset.Zero
rotation = 0F
})
}
)
Expand All @@ -154,10 +155,12 @@ fun BoxScope.UpperImage() {
fun BoxScope.LowerImage() {
var imageScale by remember { mutableFloatStateOf(1f) }
var imageOffset by remember { mutableStateOf(Offset.Zero) }
var rotation by remember { mutableFloatStateOf(0f) }

val transformState = rememberTransformableState { zoomChange, panChange, rotationChange ->
imageScale = (imageScale * zoomChange).coerceIn(1f, 5f)
imageScale = (imageScale * zoomChange).coerceIn(0.5f, 5f)
imageOffset += panChange
rotation += rotationChange
}

val model = ImageRequest.Builder(LocalContext.current)
Expand All @@ -180,18 +183,19 @@ fun BoxScope.LowerImage() {
.fillMaxWidth()
.scale(imageScale)
.graphicsLayer {
Log.d("UI", "ImageViewerScreen: graphicsLayer: scale $scaleX $scaleY")
scaleX = imageScale
scaleY = imageScale
translationX = imageOffset.x
translationY = imageOffset.y
rotationZ = rotation
}
.transformable(transformState)
.pointerInput(Unit) {
detectTapGestures(onDoubleTap = {
Log.d("UI", "ImageViewerScreen: on double tap")
imageScale = 1f
imageOffset = Offset.Zero
rotation = 0F
})
}
)
Expand Down

0 comments on commit b3eca8d

Please sign in to comment.