-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathTextBuilder.java
126 lines (105 loc) · 2.72 KB
/
TextBuilder.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package su.levenetc.android.textsurface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.TypedValue;
import su.levenetc.android.textsurface.common.Position;
import su.levenetc.android.textsurface.contants.Pivot;
/**
* Created by Eugene Levenetc.
*/
public class TextBuilder {
private String text;
private int showTime;
private Position position = new Position();
private RectF padding = new RectF();
private Paint paint = new Paint();
private float scale = 1;
private int scalePivot = Pivot.CENTER;
private TextBuilder(String text) {
this.text = text;
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setTextSize(110);
setSize(18);
}
public static TextBuilder create(String text) {
return new TextBuilder(text);
}
public TextBuilder setShowTime(int showTime) {
this.showTime = showTime;
return this;
}
public TextBuilder setPosition(float px, float py) {
position = new Position(new PointF(px, py));
return this;
}
public TextBuilder setPosition(int align) {
position = new Position(align);
return this;
}
public TextBuilder setPosition(int align, Text alignText) {
position = new Position(align, alignText);
return this;
}
/**
* params are in pixels
*/
public TextBuilder setPadding(float left, float top, float right, float bottom) {
padding.set(left, top, right, bottom);
return this;
}
/**
* params are in pixels
*/
public TextBuilder setLeftPadding(float left) {
padding.set(left, padding.top, padding.right, padding.bottom);
return this;
}
public TextBuilder setPadding(RectF padding) {
this.padding.set(padding);
return this;
}
public TextBuilder setPosition(Position position) {
this.position.set(position);
return this;
}
/**
* Overrides all previously set paint properties
*/
public TextBuilder setPaint(Paint paint) {
this.paint = new Paint(paint);
return this;
}
/**
* @param alpha from 0 to 255
*/
public TextBuilder setAlpha(int alpha) {
this.paint.setAlpha(alpha);
return this;
}
public TextBuilder setColor(int color) {
this.paint.setColor(color);
return this;
}
public TextBuilder setSize(float sp) {
paint.setTextSize(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sp, Resources.getSystem().getDisplayMetrics())
);
return this;
}
public TextBuilder setScale(float scale, int scalePivot) {
this.scale = scale;
this.scalePivot = scalePivot;
return this;
}
public Text build() {
Text result = new Text(this.text, position, padding, paint);
result.setScaleX(scale);
result.setScaleY(scale);
result.setScalePivot(scalePivot, scalePivot);
return result;
}
}