From 631b56939283463383130d741e074df2cba837d4 Mon Sep 17 00:00:00 2001 From: atyamash Date: Sun, 25 Sep 2022 00:36:31 +0900 Subject: [PATCH 1/2] zoom floor map --- .../confsched2022/feature/map/Map.kt | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt b/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt index 0467d9239..edb0c4d7d 100644 --- a/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt +++ b/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt @@ -2,14 +2,22 @@ package io.github.droidkaigi.confsched2022.feature.map import androidx.compose.foundation.Image import androidx.compose.foundation.background +import androidx.compose.foundation.gestures.rememberTransformableState +import androidx.compose.foundation.gestures.transformable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import dev.icerock.moko.resources.compose.stringResource @@ -55,10 +63,27 @@ fun Map( .padding(innerPadding), contentAlignment = Alignment.Center ) { + val minScale = 1f + val maxScale = 3f + var scale by remember { mutableStateOf(minScale) } + var offset by remember { mutableStateOf(Offset.Zero) } + val state = rememberTransformableState { zoomChange, offsetChange, _ -> + scale *= zoomChange + offset += offsetChange + } Image( painter = painterResource(id = R.drawable.map), contentDescription = "Floor map", - modifier = Modifier.padding(16.dp) + modifier = Modifier + .transformable(state = state) + .graphicsLayer( + scaleX = maxOf(minScale, minOf(maxScale, scale)), + scaleY = maxOf(minScale, minOf(maxScale, scale)), + translationX = offset.x, + translationY = offset.y, + ) + .fillMaxSize() + .padding(16.dp) ) } } From af19471108e9f717524d1781a1e4f491dcc62f0e Mon Sep 17 00:00:00 2001 From: atyamash Date: Sun, 25 Sep 2022 12:29:14 +0900 Subject: [PATCH 2/2] use coerceIn --- .../io/github/droidkaigi/confsched2022/feature/map/Map.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt b/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt index edb0c4d7d..9c020d448 100644 --- a/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt +++ b/feature/map/src/main/java/io/github/droidkaigi/confsched2022/feature/map/Map.kt @@ -77,8 +77,8 @@ fun Map( modifier = Modifier .transformable(state = state) .graphicsLayer( - scaleX = maxOf(minScale, minOf(maxScale, scale)), - scaleY = maxOf(minScale, minOf(maxScale, scale)), + scaleX = scale.coerceIn(minScale, maxScale), + scaleY = scale.coerceIn(minScale, maxScale), translationX = offset.x, translationY = offset.y, )