Skip to content

Commit 85e60db

Browse files
add polygon shape function
1 parent fb2a038 commit 85e60db

File tree

1 file changed

+25
-0
lines changed
  • cropper/src/main/java/com/smarttoolfactory/cropper/util

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.smarttoolfactory.cropper.util
22

3+
import android.graphics.Matrix
34
import androidx.compose.foundation.shape.GenericShape
45
import androidx.compose.ui.geometry.Offset
56
import androidx.compose.ui.geometry.Rect
67
import androidx.compose.ui.geometry.Size
78
import androidx.compose.ui.graphics.Path
9+
import androidx.compose.ui.graphics.asAndroidPath
810
import androidx.compose.ui.unit.LayoutDirection
911
import com.smarttoolfactory.cropper.model.AspectRatio
1012
import kotlin.math.cos
@@ -26,6 +28,7 @@ import kotlin.math.sin
2628
* ```
2729
*/
2830
fun createPolygonPath(cx: Float, cy: Float, sides: Int, radius: Float): Path {
31+
2932
val angle = 2.0 * Math.PI / sides
3033

3134
return Path().apply {
@@ -43,6 +46,28 @@ fun createPolygonPath(cx: Float, cy: Float, sides: Int, radius: Float): Path {
4346
}
4447
}
4548

49+
50+
/**
51+
* Create a polygon shape
52+
*/
53+
fun createPolygonShape(sides: Int, degrees: Float = 0f): GenericShape {
54+
return GenericShape { size: Size, _: LayoutDirection ->
55+
56+
val radius = size.width.coerceAtMost(size.height) / 2
57+
addPath(
58+
createPolygonPath(
59+
cx = size.width / 2,
60+
cy = size.height / 2,
61+
sides = sides,
62+
radius = radius
63+
)
64+
)
65+
val matrix = Matrix()
66+
matrix.postRotate(degrees, size.width / 2, size.height / 2)
67+
this.asAndroidPath().transform(matrix)
68+
}
69+
}
70+
4671
/**
4772
* Creates a [Rect] shape with given aspect ratio.
4873
*/

0 commit comments

Comments
 (0)