Android's Canvas provides a drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) method, which can draw text along the Path path, where text refers to the text content, the hOffset parameter specifies the horizontal offset, and the vOffset specifies the Vertical offset, the following is a simple example to demonstrate, the code is as follows:
Activity:
package com.lovo.testcanvasactivity; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.os.Bundle; import android.view.View; public class TestPathActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(new MyView(this)); } class MyView extends View { final String DRAW_STR = "Tianxingjian, the gentleman strives for self-improvement"; // brush private Paint paint; // declare an array of path objects Path[] paths = new Path[3]; public MyView(Context context) { super(context); paths[0] = new Path(); paths[0].moveTo(0, 0); for (int i = 0; i <= 7; i++) { // Generate 7 points, randomly generate Y coordinates, and connect them into a line paths[0].lineTo(i * 30, (float) Math.random() * 30); } paths[1] = new Path(); RectF rectF = new RectF(0, 0, 200, 120); paths[1].addOval(rectF, Path.Direction.CCW); paths[2] = new Path(); paths[2].addArc(rectF, 60, 180); // initialize the brush paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.CYAN); paint.setStrokeWidth(1); } @Override protected void onDraw(Canvas canvas) { // fill the background with white canvas.drawColor(Color.WHITE); canvas.translate(40, 40); // Set to start drawing from the right (right-aligned) paint.setTextAlign(Paint.Align.RIGHT); paint.setTextSize(20); // draw the path paint.setStyle(Paint.Style.STROKE); canvas.drawPath(paths[0], paint); // draw a piece of text along the path paint.setStyle(Paint.Style.FILL); canvas.drawTextOnPath(DRAW_STR, paths[0], -8, 20, paint); // Move the canvas down 120 canvas.translate(0, 120); // draw the path paint.setStyle(Paint.Style.STROKE); canvas.drawPath(paths[1], paint); // draw a piece of text along the path paint.setStyle(Paint.Style.FILL); canvas.drawTextOnPath(DRAW_STR, paths[1], -20, 20, paint); // Move the canvas down 120 canvas.translate(0, 120); // draw the path paint.setStyle(Paint.Style.STROKE); canvas.drawPath(paths[2], paint); // draw a piece of text along the path paint.setStyle(Paint.Style.FILL); canvas.drawTextOnPath(DRAW_STR, paths[2], -10, 20, paint); } } }
Attached picture effect: