/
Practice04SetTypefaceView.java
50 lines (40 loc) · 1.56 KB
/
Practice04SetTypefaceView.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
50
package com.geekholt.practiceui.view.lesson3;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class Practice04SetTypefaceView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
String text = "Hello Geekholt";
Typeface typeface;
public Practice04SetTypefaceView(Context context) {
super(context);
}
public Practice04SetTypefaceView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice04SetTypefaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
paint.setTextSize(60);
typeface = Typeface.createFromAsset(getContext().getAssets(), "Satisfy-Regular.ttf");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 使用 Paint.setTypeface() 来设置不同的字体
// 第一处:填入 null 来设置默认字体
paint.setTypeface(null);
canvas.drawText(text, 50, 100, paint);
paint.setTypeface(Typeface.SERIF);
// 第二处:填入 Typeface.SERIF 来设置衬线字体(宋体)
canvas.drawText(text, 50, 200, paint);
paint.setTypeface(typeface);
// 第三处:填入 typeface 对象来使用 assets 目录下的 "Satisfy-Regular.ttf" 文件
canvas.drawText(text, 50, 300, paint);
}
}