android-如何水平旋转由drawTextonPath创建的圆的文本?
作者:互联网
我有一个函数使用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();
}
解决方法:
解决方案不是使用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);
标签:canvas,drawtext,android 来源: https://codeday.me/bug/20191118/2026466.html