如何在圆圈中水平旋转drawTextonPath创建的文本?

时间:2016-09-02 16:47:59

标签: android canvas drawtext

我有一个用drawTextOnPath在画布上绘制文本的函数。我计算了偏移一切正常,但我想以特定的方式绘制它。目前文本旋转偏移等于圆偏移。我想将文字旋转90/45度。但我无法弄清楚如何。

请任何想法。

private void drawLegend(Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.rotate(-228, centerX, centerY);

    Path circle = new Path();
    double halfCircumference = (radius * 2 * Math.PI) - ((radius * 2 * Math.PI) / 8) * 2;
    double increments = 5;

    for (int i = 0; i <= this.mMaxSpeed; i += increments) {
        circle.addCircle(centerX, centerY, radius, Path.Direction.CW);
        canvas.drawTextOnPath(String.format("%d", i),
                circle,
                (float) (i * halfCircumference / this.mMaxSpeed),
                -20f,
                scalePaint);
    }
    canvas.restore();
}

This is current mapping This is current mapping

Thi is how I wnat it to be displayed Thi is how I wnat it to be displayed

1 个答案:

答案 0 :(得分:3)

解决方案不是使用drawTextOnPath进行绘制,而是使用drawText在画布上绘制

        canvas.drawText(String.valueOf(0), (x - width / 2) + 35, (y + height / 2) - 25, scalePaint);
        canvas.drawText(String.valueOf(5), (x - width / 2) - 20, (y) + 8, scalePaint);
        canvas.drawText(String.valueOf(10), (x - width / 2) + 30, (y - height / 2) + 40, scalePaint);
        canvas.drawText(String.valueOf(15), (x), (y - height / 2) - 18, scalePaint);
        canvas.drawText(String.valueOf(20), (x + width / 2) - 30, (y  - height / 2) + 40, scalePaint);
        canvas.drawText(String.valueOf(25), (x + width / 2) + 25, (y) + 8, scalePaint);
        canvas.drawText(String.valueOf(30), (x + width / 2) - 35, (y + height / 2) - 25, scalePaint);

7个月后更新

秘诀是使用sincos函数计算xy的偏移量。

double theta = (7 * PI / 4) - (i * (2 * PI / 40));
double xPos = radiusScale * 1.2 * Math.sin(theta) + centerX - widthText / 2;
double yPos = radiusScale * 1.2 * Math.cos(theta) + centerY + heightText / 2;

canvas.drawText(textNotch, (float)xPos, (float)yPos, scalePaint);