Skip to content

Commit 57469d9

Browse files
fix CropAgent path crop calculation
1 parent c4ef5c6 commit 57469d9

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class CropAgent {
9797
cropRect.height / pathSize.height
9898
)
9999
this.asAndroidPath().transform(matrix)
100+
101+
val left = getBounds().left
102+
val top = getBounds().top
103+
104+
translate(Offset(-left, -top))
100105
}
101106

102107
Canvas(image = imageToCrop).run {

cropper/src/main/java/com/smarttoolfactory/cropper/util/ShapeUtils.kt

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,36 @@ fun buildOutline(
125125
size: Size,
126126
layoutDirection: LayoutDirection,
127127
density: Density
128-
): Triple<Float, Float, Outline> {
128+
): Pair<Offset, Outline> {
129+
130+
val (shapeSize, offset) = calculateSizeAndOffsetFromAspectRatio(aspectRatio, coefficient, size)
131+
132+
val outline = shape.createOutline(
133+
size = shapeSize,
134+
layoutDirection = layoutDirection,
135+
density = density
136+
)
137+
return Pair(offset, outline)
138+
}
139+
140+
141+
/**
142+
* Calculate new size and offset based on [size], [coefficient] and [aspectRatio]
143+
*
144+
* For 4/3f aspect ratio with 1000px width, 1000px height with coefficient 1f
145+
* it returns Size(1000f, 750f), Offset(0f, 125f).
146+
*/
147+
fun calculateSizeAndOffsetFromAspectRatio(
148+
aspectRatio: AspectRatio,
149+
coefficient: Float,
150+
size: Size,
151+
): Pair<Size, Offset> {
129152
val width = size.width
130153
val height = size.height
131154

132155
val value = aspectRatio.value
133156

134-
val shapeSize = if (aspectRatio == AspectRatio.Unspecified) {
157+
val newSize = if (aspectRatio == AspectRatio.Unspecified) {
135158
Size(width * coefficient, height * coefficient)
136159
} else if (value > 1) {
137160
Size(
@@ -142,13 +165,8 @@ fun buildOutline(
142165
Size(width = coefficient * height * value, height = coefficient * height)
143166
}
144167

145-
val left = (width - shapeSize.width) / 2
146-
val top = (height - shapeSize.height) / 2
168+
val left = (width - newSize.width) / 2
169+
val top = (height - newSize.height) / 2
147170

148-
val outline = shape.createOutline(
149-
size = shapeSize,
150-
layoutDirection = layoutDirection,
151-
density = density
152-
)
153-
return Triple(left, top, outline)
154-
}
171+
return Pair(newSize, Offset(left, top))
172+
}

0 commit comments

Comments
 (0)