我有一个用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();
}
答案 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个月后更新
秘诀是使用sin
和cos
函数计算x
和y
的偏移量。
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);