Skip to content

Commit 249de5b

Browse files
Merge pull request #23 from Nimrodda/custom-grid
Added an option to draw a custom grid
2 parents d99d1eb + 2e26e88 commit 249de5b

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

app/src/main/java/com/smarttoolfactory/composecropper/demo/ImageCropDemo.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,14 @@ private fun MainContent(
189189
crop = crop,
190190
onCropStart = {
191191
isCropping = true
192+
},
193+
onCropSuccess = {
194+
croppedImage = it
195+
isCropping = false
196+
crop = false
197+
showDialog = true
192198
}
193-
) {
194-
croppedImage = it
195-
isCropping = false
196-
crop = false
197-
showDialog = true
198-
}
199+
)
199200
}
200201

201202
BottomAppBar(

app/src/main/java/com/smarttoolfactory/composecropper/demo/ImageCropDemoSimple.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ fun ImageCropDemoSimple() {
6868
cropStyle = cropStyle,
6969
cropProperties = cropProperties,
7070
crop = crop,
71-
onCropStart = {}
72-
) {
73-
}
71+
onCropStart = {},
72+
onCropSuccess = {},
73+
)
7474
}
7575
}
7676
}

cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ fun ImageCropper(
5050
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
5151
crop: Boolean = false,
5252
onCropStart: () -> Unit,
53-
onCropSuccess: (ImageBitmap) -> Unit
53+
onCropSuccess: (ImageBitmap) -> Unit,
54+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)? = null,
5455
) {
5556

5657
ImageWithConstraints(
@@ -178,7 +179,8 @@ fun ImageCropper(
178179
cropType = cropType,
179180
cropOutline = cropOutline,
180181
cropStyle = cropStyle,
181-
transparentColor = transparentColor
182+
transparentColor = transparentColor,
183+
onDrawGrid = onDrawGrid,
182184
)
183185
}
184186
}
@@ -199,6 +201,7 @@ private fun ImageCropper(
199201
cropStyle: CropStyle,
200202
overlayRect: Rect,
201203
transparentColor: Color,
204+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
202205
) {
203206
Box(
204207
modifier = Modifier
@@ -223,7 +226,8 @@ private fun ImageCropper(
223226
handleSize = handleSize,
224227
cropStyle = cropStyle,
225228
rectOverlay = overlayRect,
226-
transparentColor = transparentColor
229+
transparentColor = transparentColor,
230+
onDrawGrid = onDrawGrid,
227231
)
228232
}
229233

@@ -259,7 +263,8 @@ private fun ImageCropperImpl(
259263
handleSize: Float,
260264
cropStyle: CropStyle,
261265
transparentColor: Color,
262-
rectOverlay: Rect
266+
rectOverlay: Rect,
267+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
263268
) {
264269

265270
Box(contentAlignment = Alignment.Center) {
@@ -292,6 +297,7 @@ private fun ImageCropperImpl(
292297
drawHandles = drawHandles,
293298
handleSize = handleSize,
294299
transparentColor = transparentColor,
300+
onDrawGrid = onDrawGrid,
295301
)
296302

297303
}

cropper/src/main/java/com/smarttoolfactory/cropper/draw/Overlay.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ internal fun DrawingOverlay(
3333
rect: Rect,
3434
cropOutline: CropOutline,
3535
drawGrid: Boolean,
36-
transparentColor:Color,
36+
transparentColor: Color,
3737
overlayColor: Color,
3838
handleColor: Color,
3939
strokeWidth: Dp,
4040
drawHandles: Boolean,
41-
handleSize: Float
41+
handleSize: Float,
42+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?
4243
) {
4344
val density = LocalDensity.current
4445
val layoutDirection: LayoutDirection = LocalLayoutDirection.current
@@ -68,7 +69,8 @@ internal fun DrawingOverlay(
6869
drawHandles = drawHandles,
6970
handleSize = handleSize,
7071
pathHandles = pathHandles,
71-
outline = outline
72+
outline = outline,
73+
onDrawGrid = onDrawGrid,
7274
)
7375
}
7476
is CropPath -> {
@@ -92,7 +94,8 @@ internal fun DrawingOverlay(
9294
drawHandles = drawHandles,
9395
handleSize = handleSize,
9496
pathHandles = pathHandles,
95-
path = path
97+
path = path,
98+
onDrawGrid = onDrawGrid,
9699
)
97100
}
98101
is CropImageMask -> {
@@ -110,7 +113,8 @@ internal fun DrawingOverlay(
110113
drawHandles = drawHandles,
111114
handleSize = handleSize,
112115
pathHandles = pathHandles,
113-
image = imageBitmap
116+
image = imageBitmap,
117+
onDrawGrid = onDrawGrid,
114118
)
115119
}
116120
}
@@ -130,6 +134,7 @@ private fun DrawingOverlayImpl(
130134
handleSize: Float,
131135
pathHandles: Path,
132136
outline: Outline,
137+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
133138
) {
134139
Canvas(modifier = modifier) {
135140
drawOverlay(
@@ -142,7 +147,8 @@ private fun DrawingOverlayImpl(
142147
strokeWidth,
143148
drawHandles,
144149
handleSize,
145-
pathHandles
150+
pathHandles,
151+
onDrawGrid,
146152
) {
147153
drawCropOutline(outline = outline)
148154
}
@@ -163,6 +169,7 @@ private fun DrawingOverlayImpl(
163169
handleSize: Float,
164170
pathHandles: Path,
165171
path: Path,
172+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
166173
) {
167174
Canvas(modifier = modifier) {
168175
drawOverlay(
@@ -175,7 +182,8 @@ private fun DrawingOverlayImpl(
175182
strokeWidth,
176183
drawHandles,
177184
handleSize,
178-
pathHandles
185+
pathHandles,
186+
onDrawGrid,
179187
) {
180188
drawCropPath(path)
181189
}
@@ -196,6 +204,7 @@ private fun DrawingOverlayImpl(
196204
handleSize: Float,
197205
pathHandles: Path,
198206
image: ImageBitmap,
207+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
199208
) {
200209
Canvas(modifier = modifier) {
201210
drawOverlay(
@@ -208,7 +217,8 @@ private fun DrawingOverlayImpl(
208217
strokeWidth,
209218
drawHandles,
210219
handleSize,
211-
pathHandles
220+
pathHandles,
221+
onDrawGrid,
212222
) {
213223
drawCropImage(rect, image)
214224
}
@@ -226,7 +236,8 @@ private fun DrawScope.drawOverlay(
226236
drawHandles: Boolean,
227237
handleSize: Float,
228238
pathHandles: Path,
229-
drawBlock: DrawScope.() -> Unit
239+
onDrawGrid: (DrawScope.(rect: Rect, strokeWidth: Float, color: Color) -> Unit)?,
240+
drawBlock: DrawScope.() -> Unit,
230241
) {
231242
drawWithLayer {
232243

@@ -239,11 +250,15 @@ private fun DrawScope.drawOverlay(
239250
}
240251

241252
if (drawGrid) {
242-
drawGrid(
243-
rect = rect,
244-
strokeWidth = strokeWidth,
245-
color = overlayColor
246-
)
253+
if (onDrawGrid != null) {
254+
onDrawGrid(rect, strokeWidth, overlayColor)
255+
} else {
256+
drawGrid(
257+
rect = rect,
258+
strokeWidth = strokeWidth,
259+
color = overlayColor,
260+
)
261+
}
247262
}
248263
}
249264

0 commit comments

Comments
 (0)