-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathTextSurface.java
100 lines (82 loc) · 2.77 KB
/
TextSurface.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
package su.levenetc.android.textsurface;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
import su.levenetc.android.textsurface.animations.AnimationsSet;
import su.levenetc.android.textsurface.contants.TYPE;
import su.levenetc.android.textsurface.interfaces.ICameraAnimation;
import su.levenetc.android.textsurface.interfaces.ISet;
import su.levenetc.android.textsurface.interfaces.ISurfaceAnimation;
import su.levenetc.android.textsurface.interfaces.ITextSurfaceAnimation;
/**
* Created by Eugene Levenetc.
*/
public class TextSurface extends FrameLayout {
private TreeSet<Text> textsTree = new TreeSet<>();
private SurfaceCamera camera = new SurfaceCamera();
private ISurfaceAnimation currentAnimation = null;
public TextSurface(Context context) {
super(context);
config();
}
public TextSurface(Context context, AttributeSet attrs) {
super(context, attrs);
config();
}
private void config() {
setWillNotDraw(false);
}
public SurfaceCamera getCamera() {
return camera;
}
public void play(TYPE type, ISurfaceAnimation... animations) {
play(new AnimationsSet(type, animations));
}
public void play(ISurfaceAnimation... animations) {
play(new AnimationsSet(TYPE.PARALLEL, animations));
}
public void play(ISurfaceAnimation animation) {
configAnimations(animation);
animation.setTextSurface(this);
layout();
currentAnimation = animation;
currentAnimation.start(null);
}
private void layout() {
Iterator<Text> iterator = textsTree.iterator();
while (iterator.hasNext()) iterator.next().layout(this);
}
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
private void configAnimations(ISurfaceAnimation animation) {
if (animation instanceof ICameraAnimation) {
((ICameraAnimation) animation).setCamera(camera);
} else if (animation instanceof ISet) {
LinkedList<ISurfaceAnimation> animations = ((ISet) animation).getAnimations();
for (ISurfaceAnimation a : animations) configAnimations(a);
} else if (animation instanceof ITextSurfaceAnimation) {
ITextSurfaceAnimation textAnimation = (ITextSurfaceAnimation) animation;
Text text = textAnimation.getText();
if (text != null && textsTree.add(text)) textAnimation.setInitValues(text);
}
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
camera.onDraw(canvas);
for (Text text : textsTree) text.onDraw(canvas, this);
}
public void reset() {
if (currentAnimation != null) {
currentAnimation.cancel();
currentAnimation = null;
}
textsTree.clear();
camera.reset();
invalidate();
}
}