2

我在下面有以下代码。

public class CompassActivity extends Activity {  

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

    public OuterCircle(Context context) {
      super(context);
      init();
    }

    private void init() {
      paint.setColor(Color.WHITE);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
    }

    private void drawDegreesOnCircle(Canvas c) {
      path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
      c.drawTextOnPath(s, path, 0, 10, paint);
    }

    public void onDraw(Canvas c) {      
      int cx = getWidth()/2;
      int cy = getHeight()/2;
      c.drawCircle(cx, cy, 170, paint);      
      drawDegreesOnCircle(c);
    } 
  }
}

圆圈绘制成功。但是,我指定的字符串没有显示。代码中没有错误或警告。我的代码中是否缺少任何内容?我正在尝试在圆圈周围显示字符串。我被困在这里。:D

4

3 回答 3

2

我通过添加解决了上述问题

setLayerType(View.LAYER_TYPE_SOFTWARE, null)

上述方法需配合使用

Canvas.drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)

适用于 Android API 级别 11 或更高级别。字符串现在成功显示在圆圈周围。这是正确的代码。

public class CompassActivity extends Activity {  

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

    public OuterCircle(Context context) {
      super(context);
      init();
    }

    private void init() {
      paint.setColor(Color.WHITE);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
    }

    private void drawDegreesOnCircle(Canvas c) {
      path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
      c.drawTextOnPath(s, path, 0, 10, paint);
      setLayerType(View.LAYER_TYPE_SOFTWARE, null); // Required for API level 11 or higher.
    }

    public void onDraw(Canvas c) {      
      int cx = getWidth()/2;
      int cy = getHeight()/2;
      c.drawCircle(cx, cy, 170, paint);      
      drawDegreesOnCircle(c);
    } 
  }
}
于 2013-09-23T10:13:26.887 回答
0

你错过了一个电话drawPath()

private void drawDegreesOnCircle(Canvas c) {
  path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
  c.drawPath(path, paint);
  c.drawTextOnPath(s, path, 0, 10, paint);
}
于 2013-09-21T14:03:24.743 回答
0

一个非常简单的示例,只是为了在显示屏中心以一定角度获取文本。

public class DrawSomeText extends View {
  Paint mPaint;
  public DrawSomeText(Context context) {
    super(context);
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);
  }

  @Override
  public void onDraw(Canvas canvas) {
    Path path = new Path();
    path.moveTo(getWidth()/2, getHeight()/2);
    path.lineTo(getWidth(), getHeight());
    path.close();

    canvas.drawPath(path, mPaint);

    canvas.drawTextOnPath("Hello World", path, 0, 0, mPaint);
  }
}
于 2014-07-13T11:50:03.757 回答