0

I want to show a text in the circle as a curve, so I wrote this code:

public class CircularTextView extends View {

private String text = "";
private Path circle;
private Paint tPaint;

int xAxis = 0, yAxis = 0;


public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

private Path _arc;

private Paint _paintText;

public CircularTextView(Context context, int cx, int cy) {
    super(context);
    this.xAxis = cx;
    this.yAxis = cy;

    _arc = new Path();
    RectF oval = new RectF(xAxis,yAxis,0,yAxis/2);
    _arc.addArc(oval, 0, 180);
    _paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
    _paintText.setStyle(Paint.Style.FILL_AND_STROKE);
    _paintText.setColor(Color.WHITE);
    _paintText.setTextSize(Functions.convertDpToPixel(16, context));
    _paintText.setTypeface(ResourcesCompat.getFont(context, R.font.quick_regular));

    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawTextOnPath(getText(), _arc, xAxis/2, yAxis, _paintText);
    invalidate();
}

For using this custom class i have written this code.

 frameLayout.post(new Runnable() {
            @Override
            public void run() {

                int cx = frameLayout.getMeasuredWidth() / 2;
                int cy = frameLayout.getMeasuredHeight() / 2;

                CircularTextView circularTextView = new CircularTextView(FreddyChooseActivity.this, cx, cy);
                circularTextView.setLayoutParams(layoutParams);
                circularTextView.setText(choiceData.get(finalI).getChoiceName());

                frameLayout.addView(circularTextView);
            }
        });

above code is not working and it shows straight text not curved.

I want like this.

enter image description here

Advanced help would be appreciated!

Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
Piyush
  • 395
  • 5
  • 25

0 Answers0