/
Practice11GetFontSpacingView.java
43 lines (32 loc) · 1.16 KB
/
Practice11GetFontSpacingView.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
package com.geekholt.practiceui.view.lesson3;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class Practice11GetFontSpacingView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
String text = "Hello Geekholt";
public Practice11GetFontSpacingView(Context context) {
super(context);
}
public Practice11GetFontSpacingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice11GetFontSpacingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
paint.setTextSize(60);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 使用 Paint.getFontSpacing() 来获取推荐的行距
float spacing = paint.getFontSpacing();
canvas.drawText(text, 50, 100, paint);
canvas.drawText(text, 50, 100 + spacing, paint);
canvas.drawText(text, 50, 100 + spacing * 2, paint);
}
}