Created
March 30, 2022 14:28
-
-
Save zgsbrgr/1778ce209f7c4fcf8a12f7010c458bba to your computer and use it in GitHub Desktop.
Custom Rectangle Shape with a missing edge in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.ui.geometry.Size | |
import androidx.compose.ui.graphics.Outline | |
import androidx.compose.ui.graphics.Path | |
import androidx.compose.ui.graphics.Shape | |
import androidx.compose.ui.unit.Density | |
import androidx.compose.ui.unit.LayoutDirection | |
enum class OpenedEdge { | |
Top, | |
Start, | |
End, | |
Bottom | |
} | |
/** | |
* Custom RectangleShape where one edge is 'missing' | |
* | |
* ------- | |
* | | |
* ------- | |
* | |
* ------- | |
* | | |
* ------- | |
* | |
* | | | |
* ------- | |
* | |
* ------- | |
* | | | |
* | |
*/ | |
class OpenedRectangleShape(private val openedEdge: OpenedEdge): Shape { | |
override fun createOutline( | |
size: Size, | |
layoutDirection: LayoutDirection, | |
density: Density | |
): Outline { | |
return Outline.Generic( | |
path = drawOpenedRectanglePath(size = size, openedEdge = openedEdge) | |
) | |
} | |
private fun drawOpenedRectanglePath(size: Size, openedEdge: OpenedEdge): Path { | |
return Path().apply { | |
reset() | |
when(openedEdge) { | |
OpenedEdge.Top -> { | |
lineTo(0f,size.height) | |
lineTo(size.width, size.height) | |
lineTo(size.width, 0f) | |
} | |
OpenedEdge.Bottom -> { | |
moveTo(0f, size.height) | |
lineTo(0f, 0f) | |
lineTo(size.width, 0f) | |
lineTo(size.width, size.height) | |
} | |
OpenedEdge.End -> { | |
moveTo(size.width, 0f) | |
lineTo(0f, 0f) | |
lineTo(0f, size.height) | |
lineTo(size.width, size.height) | |
} | |
OpenedEdge.Start -> { | |
lineTo(size.width, 0f) | |
lineTo(size.width, size.height) | |
lineTo(0f, size.height) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment