Rebecca Franks
12/21/2023, 11:19 AMRebecca Franks
12/21/2023, 11:26 AMZoltan Demant
12/21/2023, 11:43 AMRebecca Franks
12/21/2023, 11:58 AMZoltan Demant
12/21/2023, 12:13 PMromainguy
12/21/2023, 2:09 PMsaket
12/21/2023, 7:34 PMsaket
12/21/2023, 7:40 PMCircleShape
to an arbitrary MaterialTheme.shape.*
value. Can I use androidx.graphics:graphics-shapes
for this? The demo app is overwhelming to find this answer.Rebecca Franks
12/21/2023, 7:56 PMsaket
12/21/2023, 8:50 PMRebecca Franks
12/21/2023, 8:50 PMandrew
12/21/2023, 9:31 PMandrew
12/22/2023, 5:43 AMandrew
12/22/2023, 5:44 AMandrew
12/22/2023, 5:45 AMRebecca Franks
02/09/2024, 3:29 PMRebecca Franks
02/09/2024, 3:29 PMandrew
02/09/2024, 3:36 PMsaket
02/09/2024, 3:49 PMandrew
02/09/2024, 5:44 PMZoltan Demant
02/11/2024, 12:20 PM@JvmInline
private value class PolygonShape(
private val polygon: Density.(Size) -> RoundedPolygon,
) : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density,
): Outline {
val polygon = with(density) {
polygon(size)
}
val path = createPath(polygon.cubics)
return Generic(path)
}
private fun createPath(
cubics: List<Cubic>,
path: Path = Path(),
scale: Float = 1f,
): Path {
path.rewind()
cubics.fastFirstOrNull { true }?.let { first ->
path.moveTo(first.anchor0X * scale, first.anchor0Y * scale)
}
cubics.fastForEach { bezier ->
path.cubicTo(
bezier.control0X * scale,
bezier.control0Y * scale,
bezier.control1X * scale,
bezier.control1Y * scale,
bezier.anchor1X * scale,
bezier.anchor1Y * scale,
)
}
path.close()
return path
}
}
Rebecca Franks
02/12/2024, 11:37 AMZoltan Demant
02/13/2024, 1:58 PMmatrix.translate(1f, 1f)
then I get a rectangle with the appropiate size.. but its no longer rounded. Code is exactly like the snippet.Rebecca Franks
02/13/2024, 3:22 PMZoltan Demant
02/13/2024, 3:29 PMSurface(
modifier = modifier,
shape = Theme.structure.squircle, <-- PolygonShape
elevation = Level2,
content = {
Column(
modifier = modifier.clickable(
onClick = onClick,
).padding(
vertical = KeylineSmall,
horizontal = KeylineMedium,
),
content = {
...
},
)
},
Rebecca Franks
02/13/2024, 6:27 PMimport androidx.compose.material.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.graphics.shapes.Cubic
import androidx.graphics.shapes.RoundedPolygon
import androidx.graphics.shapes.circle
import kotlin.math.max
@Preview
@Composable
private fun Preview2() {
Surface(
modifier = Modifier,
shape = RoundedPolygonShape(RoundedPolygon.circle(5)),
content = {
Text("Hello")
}
)
}
/**
* Gets a [Path] representation for a [RoundedPolygon] shape, which can be used to draw the
* polygon.
*
* @param path an optional [Path] object which, if supplied, will avoid the function having
* to create a new [Path] object
*/
@JvmOverloads
fun RoundedPolygon.toPath(path: Path = Path()): Path {
pathFromCubics(path, cubics)
return path
}
private fun pathFromCubics(
path: Path,
cubics: List<Cubic>
) {
var first = true
path.rewind()
for (element in cubics) {
if (first) {
path.moveTo(element.anchor0X, element.anchor0Y)
first = false
}
path.cubicTo(
element.control0X, element.control0Y, element.control1X, element.control1Y,
element.anchor1X, element.anchor1Y
)
}
path.close()
}
fun RoundedPolygon.getBounds() = calculateBounds().let { Rect(it[0], it[1], it[2], it[3]) }
class RoundedPolygonShape(
private val polygon: RoundedPolygon,
private var matrix: Matrix? = null
) : Shape {
private val path = Path()
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
path.rewind()
polygon.toPath(path)
if (matrix == null) {
matrix = Matrix()
val bounds = polygon.getBounds()
val maxDimension = max(bounds.width, bounds.height)
matrix!!.scale(size.width / maxDimension, size.height / maxDimension)
matrix!!.translate(-bounds.left, -<http://bounds.top|bounds.top>)
}
path.transform(matrix!!)
return Outline.Generic(path)
}
}
Zoltan Demant
02/14/2024, 8:20 AMRoundedPolygon.Companion.rectangle()
but to no avail, the shape is kind of like an in-between of rectangle and circle. Ive attached a screenshot of the shape Id like to acheive - this is what my earlier code snippet produced, and Im still using the same polygon.Rebecca Franks
02/14/2024, 3:08 PMRoundedPolygon.rectangle(rounding = CornerRounding(0.5f, smoothing = 1f))
should give you more in line of what you are looking for - the smoothing parameter might help you here