Question

I've the following code below.

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);
    } 
  }
}

The circle is drawn successfully. However, the string I've specified is not displayed. There is no error or warning in the code. Am I missing anything in my code? I'm trying to display the string around the circle. I got stuck here. :D

Was it helpful?

Solution

I've fixed the above issue by adding

setLayerType(View.LAYER_TYPE_SOFTWARE, null)

It is required to use the above method along with the

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

for Android API level 11 or higher. The string is now displayed successfully around the circle. Here is the correct code.

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);
    } 
  }
}

OTHER TIPS

You're missing a call to 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);
}

A very simple example just to get text in an angle on the center of the display.

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);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top