如何在圆路径中旋转drawTextOnPath创建的文本

时间:2012-01-24 13:58:04

标签: android android-canvas

无法弄清楚如何旋转文字,因此起始位置不是随机的。 (我不想动画它)。我只想指定起始位置。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GraphicsView(this));
}

private static class GraphicsView extends View {
    private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and chain.";
    Path circle;
    Paint tPaint;

    public GraphicsView(Context context) {
        super(context);
        tPaint = new Paint();
        tPaint.setColor(Color.BLUE);
        tPaint.setTextSize(30);
        tPaint.setAntiAlias(true);
        //setBackgroundResource(R.drawable.background);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        circle = new Path();
        int centerX = getWidth()/2;
        int centerY = getHeight()/2; 
        circle.addCircle(centerX, centerY, Math.min(centerX, centerY), Direction.CW);
        canvas.drawTextOnPath(QUOTE, circle, 0, 30, tPaint);
    }
}

2 个答案:

答案 0 :(得分:5)

围绕圆的位移基于圆周为2 * pi弧度。 因此,中途(180)是1 * Math.PI *半径并且从顶部开始(12 o'时钟)是1.5 * Math.PI *半径。 hOffset必须是正面的。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    circle = new Path();
    int centerX = getWidth()/2;
    int centerY = getHeight()/2;
    int radius = Math.min(centerX,centerY);
    circle.addCircle(centerX, centerY, Math.min(centerX, centerY), Direction.CW);
    canvas.drawTextOnPath(QUOTE, circle, 1.5f*Math.PI*radius, 30, tPaint);
}

答案 1 :(得分:1)

您是否尝试根据documentation更改hOffset - 参数?

  

hOffset 沿着路径添加到文本开头的距离   位置