-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeWriter.java
87 lines (77 loc) · 2.89 KB
/
TypeWriter.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
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.List;
/** Based on https://gist.github.com/Antarix/6388606 :: TypeWriter */
public class TypeWriter extends TextView {
private Context mContext;
private WritingText mText;
private List<WritingText> mArrayText;
private int mIndex;
private int mArrayIndex;
private long mDelay = 115;
private long mArrayDelay = 300;
public TypeWriter(Context context) {
super(context);
mContext = context;
}
public TypeWriter(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
private Handler mHandler = new Handler();
private Runnable characterAdder = new Runnable() {
@Override
public void run() {
int len = mText.str.length();
if (mIndex <= len)
setText(mText.str.subSequence(0, mIndex));
if (++mIndex <= len)
mHandler.postDelayed(characterAdder, mDelay);
else if (mText != null && mText.fct != null
&& mText.actionMoment == WritingText.AFTER)
mText.fct.writingAction(mContext);
if (mIndex > len) {
if (mIndex == len + 1)
mArrayIndex++;
mArrayHandler.postDelayed(stringArrayAdder, mArrayDelay);
}
}
};
private Handler mArrayHandler = new Handler();
private Runnable stringArrayAdder = new Runnable() {
@Override
public void run() {
if (mArrayText != null && mArrayIndex < mArrayText.size()) {
WritingText text = mArrayText.get(mArrayIndex);
if (text.fct != null
&& text.actionMoment == WritingText.BEFORE)
text.fct.writingAction(mContext);
animateWritingText(text);
}
}
};
public void pause() {
mArrayHandler.removeCallbacks(stringArrayAdder);
mHandler.removeCallbacks(characterAdder);
}
public void restart() {
mHandler.removeCallbacks(characterAdder);
mHandler.postDelayed(characterAdder, mDelay);
}
public void animateWritingText(WritingText text) {
mText = text;
mIndex = 0;
mHandler.removeCallbacks(characterAdder);
mHandler.postDelayed(characterAdder, mDelay);
}
public void animateWritingList(List<WritingText> texts) {
mArrayText = texts;
mArrayIndex = 0;
mArrayHandler.removeCallbacks(stringArrayAdder);
mArrayHandler.postDelayed(stringArrayAdder, mArrayDelay);
}
public void setCharacterDelay(long millis) { mDelay = millis; }
public void setStringDelay(long millis) { mArrayDelay = millis; }
}