2

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

Kara
  • 6,115
  • 16
  • 50
  • 57
Ibungo
  • 1,137
  • 12
  • 23
  • Don't know what's wrong, but you seem to be adding a circle to the path every time it's drawn. You should move the addCircle to the init method. Maybe this can help http://stackoverflow.com/questions/13153201/how-to-show-circular-text-using-textview-in-android – Ishtar Sep 23 '13 at 06:39
  • 1
    Hi @Ishtar, thanks for your comment. But, I've fixed the solution by adding setLayerType() in my code. setLayerType() is required for Android API level 11 and higher. I've posted the correct answer below. I hope it helps someone. – Ibungo Sep 23 '13 at 10:01
  • Adding the [setLayerType()](http://developer.android.com/reference/android/view/View.html#setLayerType%28int,%20android.graphics.Paint%29) worked. :D – Ibungo Sep 23 '13 at 10:17

3 Answers3

2

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);
    } 
  }
}
Ibungo
  • 1,137
  • 12
  • 23
0

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);
}
Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47
  • Hi @Andrew Fielden, I've made changes in my code as you suggested. However, another circle is drawn instead of displaying the string. I'm trying to display the string around the circle I've already drawn. – Ibungo Sep 23 '13 at 05:28
0

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);
  }
}
Mattias
  • 160
  • 7