0

I'm trying to draw some text inside the svg path. I need it in the biggest part of the shape. And with normal rotation like in point 1. But currently all I have is the result like in point 2. Could someone help me to achieve the result like in point 1?

draw a path with canvas and draw a text in this path

I'm using Canvas to draw the Path:

var bitmap = Bitmap.createBitmap(
            1000,
            1000,
            Bitmap.Config.ARGB_8888)
var  canvas =  Canvas(bitmap)
canvas.drawColor(Color.LTGRAY)

var paint  = Paint()
paint.color =Color.GREEN
canvas.drawPath(p, paint)

paint.color = Color.BLACK
paint.setStyle(Paint.Style.FILL)
paint.textSize = 20f
var text= "250"
canvas.drawTextOnPath(text,
p,
50f,0f,
paint)

var rectF = RectF()
pathObject.computeBounds(rectF, true)

//the rectangle to show the rect of the Path 
paint.color = Color.TRANSPARENT
paint.setStyle(Paint.Style.STROKE)
paint.color = Color.RED
paint.strokeWidth  =2f
canvas.drawRect(rectF,paint)

1 Answers1

1

There is no simple method you can call in Android to find the centre of the largest region in the path.

What you would need to do is use an algorithm to find the largest circle that will fit inside a polygon. Then place your text at that position.

This question may be useful: Largest circle inside a non-convex polygon

First you'll need to convert your path to a polygon (set of straight line segments). Your path may already be in that form. If it isn't (ie it contains arcs and beziers etc), then you'll need to convert it to a polygon. In API 26+, you can use Path.approximate() for that. In earlier versions of Android, that process will be a bit trickier.

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181