2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jetpack Composeで点線のDividerを表示する

Posted at

よく作るのである程度パラメータで調整可能にしています。

横向き

@Composable
fun DashHorizontalDivider(
    modifier: Modifier = Modifier,
    color: Color = MaterialTheme.colorScheme.outlineVariant,
    thickness: Dp = 1.dp,
    gap: Dp = 4.dp,
    length: Dp = 8.dp
) {
    Canvas(
        modifier
            .fillMaxWidth()
            .height(thickness)
    ) {
        drawLine(
            color = color,
            strokeWidth = thickness.toPx(),
            start = Offset(0f, thickness.toPx() / 2),
            end = Offset(size.width, thickness.toPx() / 2),
            cap = StrokeCap.Square,
            pathEffect = PathEffect.dashPathEffect(
                intervals = floatArrayOf(
                    length.toPx(),
                    gap.toPx(),
                ),
            ),
        )
    }
}

縦向き

@Composable
fun DashVerticalDivider(
    modifier: Modifier = Modifier,
    color: Color = MaterialTheme.colorScheme.outlineVariant,
    thickness: Dp = 1.dp,
    gap: Dp = 4.dp,
    length: Dp = 8.dp
) {
    Canvas(
        modifier
            .fillMaxHeight()
            .width(thickness)
    ) {
        drawLine(
            color = color,
            strokeWidth = thickness.toPx(),
            start = Offset(thickness.toPx() / 2, 0f),
            end = Offset(thickness.toPx() / 2, size.height),
            cap = StrokeCap.Square,
            pathEffect = PathEffect.dashPathEffect(
                intervals = floatArrayOf(
                    length.toPx(),
                    gap.toPx(),
                ),
            ),
        )
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?