-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUtil.java
More file actions
48 lines (42 loc) · 1.24 KB
/
Util.java
File metadata and controls
48 lines (42 loc) · 1.24 KB
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
package util;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* 作者: allen on 15/11/18.
*/
public class Util {
private Util() {
}
/**
* calculates the approximate width of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @return
*/
public static int calcTextWidth(Paint paint, String demoText) {
return (int) paint.measureText(demoText);
}
/**
* calculates the approximate height of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @return
*/
public static int calcTextHeight(Paint paint, String demoText) {
Rect r = new Rect();
paint.getTextBounds(demoText, 0, demoText.length(), r);
return r.height();
}
public static float getLineHeight(Paint paint) {
Paint.FontMetrics metrics = paint.getFontMetrics();
return metrics.descent - metrics.ascent;
}
public static float getLineSpacing(Paint paint) {
Paint.FontMetrics metrics = paint.getFontMetrics();
return metrics.ascent - metrics.top + metrics.bottom;
}
}