如何在Android平台绘制图形?附当前Swift实现代码
Android 实现带圆角的矩形路径(对应Swift的UIBezierPath逻辑)
嘿,我来帮你把这段Swift里手动构建圆角矩形路径的代码转换成Android的实现,逻辑和你的Swift代码完全对齐~
在Android中,我们用Path类来构建自定义路径,和Swift的UIBezierPath作用一致。下面是对应你代码的完整实现,包含Kotlin和Java两个版本:
Kotlin 实现(推荐)
通常在自定义View的onDraw方法中完成绘制:
override fun onDraw(canvas: Canvas) { super.onDraw(canvas) val path = Path() val cornerRadius = 30f // 和你Swift代码里的半径一致 val viewWidth = width.toFloat() val viewHeight = height.toFloat() // 对应Swift的 move(to: CGPoint(x: 0.0, y: 30)) path.moveTo(0f, cornerRadius) // 绘制左上角圆角:对应Swift的addArc方法 // Android的addArc用外接矩形RectF定义圆弧范围,参数是「外接矩形、起始角度(度数)、扫过的角度(度数)」 // Swift里的180°弧度对应Android的180°度数,顺时针扫过90°完成左上角圆角 val topLeftArcRect = RectF(0f, 0f, cornerRadius * 2, cornerRadius * 2) path.addArc(topLeftArcRect, 180f, 90f) // 绘制顶部右侧直线:对应Swift的addLine(to: CGPoint(x: self.frame.size.width-30, y: 0)) path.lineTo(viewWidth - cornerRadius, 0f) // 如果你需要完整的圆角矩形,可以继续补充下面的代码(对应剩余三个圆角) // 右上角圆角 val topRightArcRect = RectF(viewWidth - cornerRadius*2, 0f, viewWidth, cornerRadius*2) path.addArc(topRightArcRect, 270f, 90f) // 右侧直线 path.lineTo(viewWidth, viewHeight - cornerRadius) // 右下角圆角 val bottomRightArcRect = RectF(viewWidth - cornerRadius*2, viewHeight - cornerRadius*2, viewWidth, viewHeight) path.addArc(bottomRightArcRect, 0f, 90f) // 底部直线 path.lineTo(cornerRadius, viewHeight) // 左下角圆角 val bottomLeftArcRect = RectF(0f, viewHeight - cornerRadius*2, cornerRadius*2, viewHeight) path.addArc(bottomLeftArcRect, 90f, 90f) // 闭合路径,让图形完整 path.close() // 设置画笔样式:描边/填充、颜色、宽度等 val paint = Paint().apply { color = Color.BLACK style = Paint.Style.STROKE // 描边模式,换成FILL就是填充模式 strokeWidth = 2f isAntiAlias = true // 抗锯齿,让边缘更平滑 } // 把路径绘制到画布上 canvas.drawPath(path, paint) }
Java 实现
如果你的项目用Java开发,逻辑完全一致,语法调整如下:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Path path = new Path(); float cornerRadius = 30f; float viewWidth = getWidth(); float viewHeight = getHeight(); // 对应Swift的moveTo path.moveTo(0f, cornerRadius); // 左上角圆角 RectF topLeftArcRect = new RectF(0f, 0f, cornerRadius * 2, cornerRadius * 2); path.addArc(topLeftArcRect, 180f, 90f); // 顶部右侧直线 path.lineTo(viewWidth - cornerRadius, 0f); // 补充完整圆角矩形的剩余部分(可选) RectF topRightArcRect = new RectF(viewWidth - cornerRadius*2, 0f, viewWidth, cornerRadius*2); path.addArc(topRightArcRect, 270f, 90f); path.lineTo(viewWidth, viewHeight - cornerRadius); RectF bottomRightArcRect = new RectF(viewWidth - cornerRadius*2, viewHeight - cornerRadius*2, viewWidth, viewHeight); path.addArc(bottomRightArcRect, 0f, 90f); path.lineTo(cornerRadius, viewHeight); RectF bottomLeftArcRect = new RectF(0f, viewHeight - cornerRadius*2, cornerRadius*2, viewHeight); path.addArc(bottomLeftArcRect, 90f, 90f); path.close(); Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2f); paint.setAntiAlias(true); canvas.drawPath(path, paint); }
关键差异说明
和你的Swift代码对比,需要注意两个核心点:
- 角度单位:Swift用弧度,Android直接用度数,不需要额外做弧度转换
- 圆弧定义方式:Swift通过圆心+半径定义圆弧,Android通过**外接矩形
RectF**定义圆弧范围,再配合起始角度和扫过角度来绘制
另外,如果只是需要简单的圆角矩形,Android也提供了更简便的canvas.drawRoundRect方法,但如果是像你这样需要手动构建路径的场景,上面的Path手动构建方式就完全匹配你的Swift代码逻辑啦~
内容的提问来源于stack exchange,提问作者Raja Sekar
相关产品推荐
相关产品推荐

