camkadev
11/25/2019, 6:38 AMcamkadev
11/25/2019, 6:42 AM@Composable
fun DrawBorder(color: Color, strokeWidth: Float) {
val paint = Paint()
paint.strokeWidth = strokeWidth
paint.color = color
Draw { canvas: Canvas, parentSize: PxSize ->
canvas.drawLine(Offset.zero, Offset(parentSize.width.value, 0f), paint)
canvas.drawLine(
Offset(parentSize.width.value - strokeWidth, 0f),
Offset(parentSize.width.value - strokeWidth, parentSize.height.value),
paint
)
canvas.drawLine(
Offset(parentSize.width.value, parentSize.height.value - strokeWidth),
Offset(0f, parentSize.height.value - strokeWidth),
paint
)
canvas.drawLine(
Offset(0f, parentSize.height.value),
Offset.zero,
paint
)
}
}
camkadev
11/25/2019, 6:42 AM@Preview
@Composable
fun MainPreview() {
MaterialTheme {
val fieldSize = 100
Container(width = fieldSize.dp, height = fieldSize.dp) {
DrawBorder(Color.Red, 1f)
Align(alignment = Alignment.CenterLeft) {
Container(width = (fieldSize / 4).dp, height = (fieldSize / 2).dp) {
DrawBorder(Color.Green, 1f)
}
}
// this one does not draw
Align(alignment = Alignment.CenterLeft) {
Container(width = (fieldSize / 6).dp, height = (fieldSize / 4).dp ) {
DrawBorder(Color.Yellow, 1f)
}
}
}
}
}
Yann Badoual
11/25/2019, 7:01 AMDrawShape
with a RectangleShape
or RoundedCornerShape
and this custom Brush:
class StrokeColor(
val color: Color,
val strokeWidth: Px
) : Brush {
override fun applyTo(p: Paint) {
p.style = PaintingStyle.stroke
p.color = color
p.strokeWidth = strokeWidth.value
}
}
Yann Badoual
11/25/2019, 7:02 AMcamkadev
11/25/2019, 8:11 AMYellow
shape does not draw anyway
val fieldSize = 100
Container(width = fieldSize.dp, height = fieldSize.dp) {
DrawShape(shape = RectangleShape, brush = StrokeColor(Color.Red, 1.px))
Align(alignment = Alignment.CenterLeft) {
Container(width = (fieldSize / 4).dp, height = (fieldSize / 2).dp) {
DrawShape(shape = RectangleShape, brush = StrokeColor(Color.Green, 1.px))
}
}
Align(alignment = Alignment.CenterLeft) {
Container(width = (fieldSize / 6).dp, height = (fieldSize / 4).dp) {
DrawShape(shape = RectangleShape, brush = StrokeColor(Color.Yellow, 1.px))
}
}
}
camkadev
11/25/2019, 8:16 AMDrawShape
inside Stack
and now it workscamkadev
11/25/2019, 8:21 AMStack
inside sized Container
then will be drawn only Red
and Yellow
shapes and Green
will be lost somewhereAndrey Kulikov
11/26/2019, 4:27 PMDrawBorder
which draws an inner stroke