Warm tip: This article is reproduced from stackoverflow.com, please click
android android-canvas android-custom-view

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

发布于 2020-05-10 20:29:35

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

Questioner
Siddarth G
Viewed
17
Siddarth G 2020-03-11 20:14

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()