Skip to content

Commit 31eb4a3

Browse files
committed
Extended the demo app with a switch to enable fixed aspect ratio
1 parent 3a76df2 commit 31eb4a3

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

app/src/main/java/com/smarttoolfactory/composecropper/preferences/CropPropertySelection.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ internal fun CropPropertySelectionMenu(
6363
}
6464
)
6565

66+
Title("Fix aspect ratio")
67+
FixedAspectRatioEnableSelection(
68+
fixedAspectRatioEnabled = cropProperties.fixedAspectRatio,
69+
onFixedAspectRatioChanged = {
70+
onCropPropertiesChange(
71+
cropProperties.copy(fixedAspectRatio = it)
72+
)
73+
}
74+
)
75+
6676
Title("Frame")
6777
CropFrameSelection(
6878
aspectRatio = aspectRatio,
@@ -193,6 +203,18 @@ internal fun FlingEnableSelection(
193203

194204
}
195205

206+
@Composable
207+
internal fun FixedAspectRatioEnableSelection(
208+
fixedAspectRatioEnabled: Boolean,
209+
onFixedAspectRatioChanged: (Boolean) -> Unit
210+
) {
211+
FullRowSwitch(
212+
label = "Enable fixed aspect ratio",
213+
state = fixedAspectRatioEnabled,
214+
onStateChange = onFixedAspectRatioChanged
215+
)
216+
}
217+
196218
@Composable
197219
internal fun PanEnableSelection(
198220
panEnabled: Boolean,

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,20 @@ fun ImageCropper(
9696

9797
val cropType = cropProperties.cropType
9898
val contentScale = cropProperties.contentScale
99-
99+
val fixedAspectRatio = cropProperties.fixedAspectRatio
100100
val cropOutline = cropProperties.cropOutlineProperty.cropOutline
101101

102102
// these keys are for resetting cropper when image width/height, contentScale or
103103
// overlay aspect ratio changes
104104
val resetKeys =
105-
getResetKeys(scaledImageBitmap, imageWidthPx, imageHeightPx, contentScale, cropType)
105+
getResetKeys(
106+
scaledImageBitmap,
107+
imageWidthPx,
108+
imageHeightPx,
109+
contentScale,
110+
cropType,
111+
fixedAspectRatio
112+
)
106113

107114
val cropState = rememberCropState(
108115
imageSize = IntSize(bitmapWidth, bitmapHeight),
@@ -337,19 +344,22 @@ private fun getResetKeys(
337344
imageWidthPx: Int,
338345
imageHeightPx: Int,
339346
contentScale: ContentScale,
340-
cropType: CropType
347+
cropType: CropType,
348+
fixedAspectRatio: Boolean,
341349
) = remember(
342350
scaledImageBitmap,
343351
imageWidthPx,
344352
imageHeightPx,
345353
contentScale,
346-
cropType
354+
cropType,
355+
fixedAspectRatio,
347356
) {
348357
arrayOf(
349358
scaledImageBitmap,
350359
imageWidthPx,
351360
imageHeightPx,
352361
contentScale,
353-
cropType
362+
cropType,
363+
fixedAspectRatio,
354364
)
355365
}

0 commit comments

Comments
 (0)