简体   繁体   English

如何在android中使用canvas画一个环?

[英]How to draw a ring using canvas in android?

Can anybody suggest me how to draw a ring using canvas methods.谁能建议我如何使用 canvas 方法画一个环。 I may draw to circles using canvas.drawCircle() but how should I feel a space between them?我可以使用canvas.drawCircle()绘制圆圈,但我应该如何感觉到它们之间的空间?

  1. You can draw a circle with a thick brush (use setStrokeWidth ).您可以用粗刷画一个圆圈(使用setStrokeWidth )。
  2. You can draw two circles, one inside another.你可以画两个圆圈,一个在另一个里面。 One filled with 'ring' color, and another (inner one) filled with screen 'background color'一个填充“环”颜色,另一个(内部)填充屏幕“背景颜色”

In kotlin you can do:在 kotlin 你可以这样做:

  • define your paint with stroke style whitin the init block在 init 块中使用描边样式定义您的绘画
class CustomView(context: Context, attrs: AttributeSet) : View(context, attrs) {

private var ringPaint: Paint
  init {
        ringPaint = Paint()
        ringPaint.color = R.color.RED // Your color here
        ringPaint.style = Paint.Style.STROKE // This is the important line
        ringPaint.strokeWidth = 20f // Your stroke width in pixels
  }

}
  • draw your circle in the onDraw method by using drawCircleFunction (centerX,centerY,radius,paint)使用 drawCircleFunction (centerX,centerY,radius,paint) 在 onDraw 方法中绘制圆
override fun draw(canvas: Canvas?) {
            super.draw(canvas)
            canvas?.drawCircle(width / 2.0f, height / 2.0f, (width - 10) / 2.0f, ringPaint)
        }

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

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