How to use android canvas to draw a Rectangle with only topleft and topright corners round?

前端 未结 15 2374
后悔当初
后悔当初 2020-11-29 01:58

I found a function for rectangles with all 4 corners being round, but I want to have just the top 2 corners round. What can I do?

canvas.drawRoundRect(new Re         


        
15条回答
  •  爱一瞬间的悲伤
    2020-11-29 02:14

    For API 21 and above the Path class added a new method addRoundRect() which you can use it like this.

    corners = new float[]{
        80, 80,        // Top left radius in px
        80, 80,        // Top right radius in px
        0, 0,          // Bottom right radius in px
        0, 0           // Bottom left radius in px
    };
    
    final Path path = new Path();
    path.addRoundRect(rect, corners, Path.Direction.CW);
    canvas.drawPath(path, mPaint);
    

    in Kotlin

    val corners = floatArrayOf(
        80f, 80f,   // Top left radius in px
        80f, 80f,   // Top right radius in px
        0f, 0f,     // Bottom right radius in px
        0f, 0f      // Bottom left radius in px
    )
    
    val path = Path()
    path.addRoundRect(rect, corners, Path.Direction.CW)
    canvas.drawPath(path, mPaint)
    

提交回复
热议问题