Search code examples
androidandroid-canvasandroid-custom-view

How to rotate text drawn in a straight line (without radius) when using drawTextOnPath?


I want to rotate the numbers in canvas,

This is what I have tried:

override fun onDraw(canvas: Canvas) {    
var i = 0
while (i < rulerHeightInInch) {
val markingPositionYaxis =
            screenHeightInPx - (ydpinch * i + topThreshold)
paint.textSize = getPixelValueForDp(18.0f)
                    val path = Path()
                    path.reset()
                    path.moveTo(
                        (getPixelValueForDp(30f) + paint.textSize),
                        markingPositionYaxis +17
                    )
                    path.lineTo(
                        (getPixelValueForDp(30f) + paint.textSize),
                        markingPositionYaxis - (paint.textSize)
                    )
                    canvas.drawTextOnPath(nf.format(i / 32), path, 0f, 0f, paint)
}
i++
}
}

First image is what I have right now

This is what I have in portrait mode from top left to bottom left:

Second image is what I want This is what I want

I don't want to draw in circular path or by using radius I want from top to bottom in a straight line


Solution

  • This is how i did it:

    canvas.save()
    canvas.rotate(180f,(getPixelValueForDp(30f) + paint.textSize),markingPositionYaxis)
    canvas.drawTextOnPath(nf.format(i / 32), path, 0f, 0f, paint)
    canvas.restore()