Skip to content

Commit 2271b53

Browse files
add ZoomableImage function that takes ZoomState as param
1 parent d9f1858 commit 2271b53

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

image/src/main/java/com/smarttoolfactory/image/zoom/ZoomableImage.kt

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,36 @@ fun ZoomableImage(
4444
contentScale: ContentScale = ContentScale.Fit,
4545
contentDescription: String? = null,
4646
alpha: Float = DefaultAlpha,
47+
colorFilter: ColorFilter? = null,
48+
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
4749
initialZoom: Float = 1f,
4850
minZoom: Float = 1f,
4951
maxZoom: Float = 5f,
5052
limitPan: Boolean = true,
5153
zoomEnabled: Boolean = true,
5254
panEnabled: Boolean = true,
5355
rotationEnabled: Boolean = false,
56+
clipTransformToContentScale: Boolean = false,
5457
consume: Boolean = true,
5558
onGestureStart: (ZoomData) -> Unit = {},
5659
onGesture: (ZoomData) -> Unit = {},
57-
onGestureEnd: (ZoomData) -> Unit = {},
58-
clipTransformToContentScale: Boolean = false,
59-
colorFilter: ColorFilter? = null,
60-
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
60+
onGestureEnd: (ZoomData) -> Unit = {}
6161
) {
6262

6363
val zoomModifier = Modifier
6464
.zoom(
65-
Unit,
66-
limitPan = limitPan,
67-
zoomEnabled = zoomEnabled,
68-
panEnabled = panEnabled,
69-
rotationEnabled = rotationEnabled,
65+
key1 = imageBitmap,
66+
key2 = contentScale,
7067
zoomState = rememberZoomState(
68+
key1 = imageBitmap,
69+
key2 = contentScale,
7170
initialZoom = initialZoom,
7271
minZoom = minZoom,
73-
maxZoom = maxZoom
72+
maxZoom = maxZoom,
73+
limitPan = limitPan,
74+
zoomEnabled = zoomEnabled,
75+
panEnabled = panEnabled,
76+
rotationEnabled = rotationEnabled,
7477
),
7578
consume = consume,
7679
onGestureStart = onGestureStart,
@@ -90,6 +93,79 @@ fun ZoomableImage(
9093
drawImage = !clipTransformToContentScale
9194
) {
9295

96+
if (clipTransformToContentScale) {
97+
Image(
98+
bitmap = imageBitmap,
99+
contentScale = contentScale,
100+
modifier = zoomModifier,
101+
alignment = alignment,
102+
contentDescription = contentDescription,
103+
alpha = alpha,
104+
colorFilter = colorFilter,
105+
filterQuality = filterQuality,
106+
)
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Zoomable image that zooms in and out in [zoomState.minZoom, zoomState.maxZoom] interval and translates
113+
* zoomed image based on pointer position.
114+
* Double tap gestures reset image translation and zoom to default values with animation.
115+
*
116+
117+
* @param clipTransformToContentScale when set true zoomable image takes borders of image drawn
118+
* while zooming in. [contentScale] determines whether will be empty spaces on edges of Composable
119+
* @param consume flag to prevent other gestures such as scroll, drag or transform to get
120+
* event propagations
121+
* @param onGestureStart callback to to notify gesture has started and return current ZoomData
122+
* of this modifier
123+
* @param onGesture callback to notify about ongoing gesture and return current ZoomData
124+
* of this modifier
125+
* @param onGestureEnd callback to notify that gesture finished and return current ZoomData
126+
* of this modifier
127+
*/
128+
@Composable
129+
fun ZoomableImage(
130+
modifier: Modifier = Modifier,
131+
imageBitmap: ImageBitmap,
132+
alignment: Alignment = Alignment.Center,
133+
contentScale: ContentScale = ContentScale.Fit,
134+
contentDescription: String? = null,
135+
alpha: Float = DefaultAlpha,
136+
colorFilter: ColorFilter? = null,
137+
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
138+
clipTransformToContentScale: Boolean = false,
139+
zoomState: ZoomState,
140+
consume: Boolean = true,
141+
onGestureStart: (ZoomData) -> Unit = {},
142+
onGesture: (ZoomData) -> Unit = {},
143+
onGestureEnd: (ZoomData) -> Unit = {}
144+
145+
) {
146+
147+
val zoomModifier = Modifier
148+
.zoom(
149+
key1 = imageBitmap,
150+
key2 = contentScale,
151+
zoomState = zoomState,
152+
consume = consume,
153+
onGestureStart = onGestureStart,
154+
onGesture = onGesture,
155+
onGestureEnd = onGestureEnd
156+
)
157+
158+
ImageWithConstraints(
159+
modifier = if (clipTransformToContentScale) modifier else modifier.then(zoomModifier),
160+
imageBitmap = imageBitmap,
161+
alignment = alignment,
162+
contentScale = contentScale,
163+
contentDescription = contentDescription,
164+
alpha = alpha,
165+
colorFilter = colorFilter,
166+
filterQuality = filterQuality,
167+
drawImage = !clipTransformToContentScale
168+
) {
93169

94170
if (clipTransformToContentScale) {
95171
Image(
@@ -106,3 +182,4 @@ fun ZoomableImage(
106182
}
107183
}
108184

185+

0 commit comments

Comments
 (0)