2

我是 Android 新手,我编写了以下示例代码。

public class Circle extends View {
    Paint paint = new Paint();
    Path path = new Path();
    private static final String s = "Hello world example";

    public Circle(Context context) {
      super(context);
      paint.setColor(Color.BLACK);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
      paint.setTextSize(30);
    }

    public void onDraw(Canvas c) {    
      path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
      c.drawTextOnPath(s, path, 0, 10, paint);
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    } 

  }

Canvas.drawTextOnPath()正在显示从右侧开始的文本(我的意思是正 X 轴)。我想从顶部顺时针显示文本。我想更改文本的起始位置。我对 AndroidCanvas.translate()Canvas.scale(). 还是我应该使用Canvas.rotate()?我已经粘贴了下面的输出,以便清楚地理解我的问题。在此处输入图像描述

我想以这种形式显示输出。

在此处输入图像描述

4

2 回答 2

2

我终于通过在应用和方法之前在我的代码中Canvas添加了我的问题。下面是代码。Canvas.rotate(float degrees, float px, float py)Canvas

public class Circle extends View {
    Paint paint = new Paint();
    Path path = new Path();
    private static final String s = "Hello world example";

    public Circle(Context context) {
      super(context);
      paint.setColor(Color.BLACK);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
      paint.setTextSize(30);
    }

    public void onDraw(Canvas c) {   
      c.rotate(-90, getWidth()/2, getHeight()/2);
      path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
      c.drawTextOnPath(s, path, 0, 10, paint);
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    } 
}

我以前试过Canvas.rotate(float degrees)但没有奏效。然而,Canvas.rotate(float degrees, float px, float py)奏效了!

于 2013-09-27T09:32:25.100 回答
2

可以使用圆弧代替圆:

Path path = new Path(); RectF rect = new RectF(width/2 - radius, height/2 - radius, width/2 + radius, height/2 + radius); path.addArc(rect 270, 270); canvas.drawTextOnPath(msg., path, 0, 0, paint);

可以使用 Matrix 平移、旋转和缩放路径。例如,上面的代码将从第一象限开始绘制文本,即从(y, 0)开始。要从 (-y, 0) 顺时针方向开始绘图,

 Path path = new Path();
 RectF rect = new RectF(width/2 - radius, height/2 - radius, width/2 + radius, height/2 + radius);
 Matrix matrix = new Matrix();
 matrix.setScale(-1, -1, width/2, height/2);
 path.addArc(rect, 270, 270);
 path.transform(matrix);
 canvas.drawTextOnPath(msg, path, 0, 0, paint);
于 2014-10-26T18:20:43.347 回答