How can I draw text as a circular pattern in Android

I want to add text in a circle or semicircle programmatically, so that instead of having a circle with the edges of the line, the edges are words. See Image for a better explanation.

text circle

How can I do this in Android or what resources can I read to help me with this problem?

+4
source share
2 answers

To do this, you need to draw some text on the Canvas . Any subclass of View is passed to Canvas in onDraw() , which you can use to draw your own text. The drawTextOnPath() method allows you to place text in any Path object that you select. You can create a semicircle path by creating a new instance and using addArc() .

+2
source

You can use the code below. and do it the way you want the text. Here, if you want something like a Backgroung image, use setBackgroundResource(R.drawable.YOUR_IMAGE);

  public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(this));} static public class GraphicsView extends View { private static final String QUOTE = "text in a half-circle"; private Path circle; private Paint cPaint; private Paint tPaint; public GraphicsView(Context context) { super(context); int color = Color.argb(127, 255, 0, 255); circle = new Path(); circle.addCircle(230, 350, 150, Direction.CW); cPaint = new Paint(Paint.ANTI_ALIAS_FLAG); cPaint.setStyle(Paint.Style.STROKE); cPaint.setColor(Color.LTGRAY); cPaint.setStrokeWidth(3); // For Background Image setBackgroundResource(R.drawable.YOUR_IMAGE); tPaint = new Paint(Paint.ANTI_ALIAS_FLAG); tPaint.setStyle(Paint.Style.FILL_AND_STROKE); //TextColor you want to set tPaint.setColor(Color.BLACK); //TextSize you want to set tPaint.setTextSize(50);} @Override protected void onDraw(Canvas canvas) { canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);} } } 

try it. Hope this helps you.

+1
source

All Articles