android 如何在Jetpack Compose中绘制垂直票据形状

l5tcr1uw  于 2023-10-14  发布在  Android
关注(0)|答案(1)|浏览(134)

我正在尝试创建一个票证形状。我有一个在X轴上用一个圆创建的源,但我在将其转换到Y轴时遇到了麻烦。如何编辑它以在左右边缘之间创建一个半圆形?
我试过了,但没成功:

private fun getTicketPathVertical(size: Size, density: Density): Path {
   val middleY = size.height.div(other = 2f) 
   val circleRadiusInPx = with(density) { circleRadius.toPx() }
    return Path().apply {
    reset()
   
    lineTo(x = 0F, y = 0F)

    lineTo(x = size.width, y = 0F)
   
    lineTo(x = size.width, y = middleY)
    
    arcTo(
        rect = Rect(
            left = size.width - circleRadiusInPx * 2,
            top = middleY - circleRadiusInPx,
            right = size.width,
            bottom = middleY + circleRadiusInPx
        ),
        startAngleDegrees = 270f,
        sweepAngleDegrees = -180F,
        forceMoveTo = false
    )
    
    lineTo(x = size.width, y = size.height)

    lineTo(0F, size.height)

    lineTo(x = 0F, y = size.height)
    arcTo(
        rect = Rect(
            left = 0F,
            top = middleY - circleRadiusInPx,
            right = circleRadiusInPx * 2,
            bottom = middleY + circleRadiusInPx
        ),
        startAngleDegrees = 90f,
        sweepAngleDegrees = -180F,
        forceMoveTo = false
    )
    
    lineTo(x = 0F, y = 0F)
 }
}

rdrgkggo

rdrgkggo1#

我解决了这个问题

private fun getTicketPathVertical(size: Size, density: Density): Path {
    val middleY = size.height.div(other = 2f) 
    val circleRadiusInPx = with(density) { circleRadius.toPx() }
    return Path().apply {
        reset()
       
        lineTo(x = 0F, y = 0F)
   
        lineTo(x = size.width, y = 0F)
        
        lineTo(x = size.width, y = middleY)
        
        arcTo(
            rect = Rect(
                left = size.width - circleRadiusInPx,
                top = middleY - circleRadiusInPx,
                right = size.width + circleRadiusInPx,
                bottom = middleY + circleRadiusInPx
            ),
            startAngleDegrees = 270f,
            sweepAngleDegrees = -180F,
            forceMoveTo = false
        )
       
        lineTo(x = size.width, y = size.height)

        lineTo(0F, size.height)

        lineTo(x = 0F, y = size.height)
        arcTo(
            rect = Rect(
                left = 0F - circleRadiusInPx,
                top = middleY - circleRadiusInPx,
                right = circleRadiusInPx,
                bottom = middleY + circleRadiusInPx
            ),
            startAngleDegrees = 90f,
            sweepAngleDegrees = -180F,
            forceMoveTo = false
        )
        
        lineTo(x = 0F, y = 0F)
    }
}

相关问题