/
Practice16TextPathView.java
49 lines (35 loc) · 1.25 KB
/
Practice16TextPathView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.geekholt.practiceui.view.lesson2;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class Practice16TextPathView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Path textPath = new Path();
String text = "Hello Geekholt";
public Practice16TextPathView(Context context) {
super(context);
}
public Practice16TextPathView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice16TextPathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
paint.setTextSize(120);
pathPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(text, 50, 200, paint);
// 使用 Paint.getTextPath() 来获取文字的 Path
paint.getTextPath(text, 0, text.length(), 50, 400, textPath);
canvas.drawPath(textPath, pathPaint);
}
}