繁体   English   中英

如何在Android画布上绘制局部圆角矩形?

[英]How to draw a partial round rect on a android canvas?

我想在画布上绘制一个用颜色(或位图)填充并带有笔触的圆角矩形 ,但带有某些选定的角,并且某些选定的侧面上没有边框 任何想法,我怎么能做到这一点?

您可以将绘图过程分为两部分。

  1. 绘制填充区域

    当尝试绘制标准sdk API不支持的形状时,Canvas.drawPath方法将是一种很好的方法。您可以定义四个变量来表示四个角的半径。

     Path path = new Path(); Rect drawingRect = {the rect area you want to draw} RectF topLeftArcBound = new RectF(); RectF topRightArcBound = new RectF(); RectF bottomLeftArcBound = new RectF(); RectF bottomRightArcBound = new RectF(); topRightArcBound.set(drawingRect.right - topRightRadius * 2, drawingRect.top, drawingRect.right, drawingRect.top + topRightRadius * 2); bottomRightArcBound.set(drawingRect.right - bottomRightRadius * 2, drawingRect.bottom - bottomRightRadius * 2, drawingRect.right, drawingRect.bottom); bottomLeftArcBound.set(drawingRect.left, drawingRect.bottom - bottomLeftRadius * 2, drawingRect.left + bottomLeftRadius * 2, drawingRect.bottom); topLeftArcBound.set(drawingRect.left, drawingRect.top, drawingRect.left + topLeftRadius * 2, drawingRect.top + topLeftRadius * 2); path.reset(); path.moveTo(drawingRect.left + topLeftRadius, drawingRect.top); //draw top horizontal line path.lineTo(drawingRect.right - topRightRadius, drawingRect.top); //draw top-right corner path.arcTo(topRightArcBound, -90, 90); //draw right vertical line path.lineTo(drawingRect.right, drawingRect.bottom - bottomRightRadius); //draw bottom-right corner path.arcTo(bottomRightArcBound, 0, 90); //draw bottom horizontal line path.lineTo(drawingRect.left - bottomLeftRadius, drawingRect.bottom); //draw bottom-left corner path.arcTo(bottomLeftArcBound, 90, 90); //draw left vertical line path.lineTo(drawingRect.left, drawingRect.top + topLeftRadius); //draw top-left corner path.arcTo(topLeftArcBound, 180, 90); path.close(); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); 

    您可以将所选角的半径设置为零

  2. 画边框

    边框包含八个部分:

    • 左上角
    • 顶线
    • 右上角
    • 右行
    • 右下角
    • 底线
    • 左下角
    • 左行

    使用int值包含选定的部分将是一个好主意

    \n 私有静态最终int TOP_LEFT_CORNER = 0x1;\n 私有静态最终int TOP_LINE = 0x2;\n 私有静态最终整数TOP_RIGHT_CORNER = 0x4;\n 私有静态最终int RIGHT_LINE = 0x8;\n 私有静态最终int BOTTOM_RIGHT_CORNER = 0x10;\n 私有静态最终int BOTTOM_LINE = 0x20;\n 私有静态最终int BOTTOM_LEFT_CORNER = 0x40;\n 私有静态最终int LEFT_LINE = 0x80;\n\n private int selectedParts = TOP_LEFT_CORNER |  TOP_LINE |  TOP_RIGHT_CORNER;\n

    现在我们可以根据selectedParts绘制边框:

    \n paint.setStyle(Paint.Style.STROKE);\n if((selectedParts和TOP_LINE)> 0){\n     canvas.drawLine(drawingRect.left + topLeftRadius,drawingRect.top,drawingRect.right-topRightRadius,drawingRect.top);\n }\n\n if((selectedParts&TOP_RIGHT_CORNER)> 0){\n     canvas.drawArc(topRightArcBound,-90,90,false,paint);\n }\n\n if((selectedParts&RIGHT_LINE)> 0){\n     canvas.drawLine(drawingRect.right,drawingRect.top + topRightRadius,drawingRect.right,drawingRect.bottom-bottomRightRadius,paint);\n }\n\n if((selectedParts&BOTTOM_RIGHT_CORNER)> 0){\n     canvas.drawArc(bottomRightArcBound,0,90,false,paint);\n }\n\n if((selectedParts&BOTTOM_LINE)> 0){\n     canvas.drawLine(drawingRect.right-bottomRightRadius,drawingRect.bottom。drawingRect.left + bottomLeftRadius,drawingRect.bottom,paint);\n }\n\n if((selectedParts&BOTTOM_LEFT_CORNER)> 0){\n     canvas.drawArc(bottomLeftArcBound,90,90,false,paint);\n }\n\n if((selectedParts&LEFT_LINE)> 0){\n     canvas.drawLine(drawingRect.left,drawingRect.bottom-bottomLeftRadius,drawingRect.left,drawingRect.top + topLeftRadius,paint);\n }\n\n if((selectedParts&TOP_LEFT_CORNER)> 0){\n     canvas.drawArc(topLeftArcBound,180,90,false,paint);\n }\n

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM