-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeWriterView.java
135 lines (112 loc) · 3.6 KB
/
TypeWriterView.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
127
128
129
130
131
132
133
134
135
package com.TypeWriter;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.Toast;
import androidx.appcompat.widget.AppCompatTextView;
/**
* Created by P SANDESH BALIGA in Roshan Hospitality, Koramangala on 21/02/2018.
*/
public class TypeWriterView extends AppCompatTextView {
private MediaPlayer mPlayer;
private CharSequence mText;
private String mPrintingText;
private int mIndex;
private long mDelay = 100; //Default 500ms delay
private Context mContext;
private TypeWriterListener mTypeWriterListener;
private boolean animating = false;
private Runnable mBlinker;
private int i = 0;
private Handler mHandler = new Handler();
private Runnable mCharacterAdder = new Runnable() {
@Override
public void run() {
if (animating) {
setText(mText.subSequence(0, mIndex++) + "_");
//typing typed
if (mTypeWriterListener != null)
mTypeWriterListener.onCharacterTyped(mPrintingText, mIndex);
if (mIndex <= mText.length()) {
mHandler.postDelayed(mCharacterAdder, mDelay);
}
}
}
};
public TypeWriterView(Context context) {
super(context);
mContext = context;
}
public TypeWriterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void callBlink() {
mBlinker = new Runnable() {
@Override
public void run() {
if (i <= 10) {
if (i++ % 2 != 0) {
setText(String.format("%s _", mText));
mHandler.postDelayed(mBlinker, 150);
} else {
setText(String.format("%s ", mText));
mHandler.postDelayed(mBlinker, 150);
}
} else
i = 0;
}
};
mHandler.postDelayed(mBlinker, 150);
}
/**
* Call this function to display
*
* @param text attribute
*/
public void animateText(String text) {
if (!animating) {
animating = true;
mText = text;
mPrintingText = text;
mIndex = 0;
setText("");
mHandler.removeCallbacks(mCharacterAdder);
//typing start
if (mTypeWriterListener != null)
mTypeWriterListener.onTypingStart(mPrintingText);
mHandler.postDelayed(mCharacterAdder, mDelay);
} else {
//CAUTION: Already typing something..
Toast.makeText(mContext, "Typewriter busy typing: " + mText, Toast.LENGTH_SHORT).show();
}
}
/**
* Call this function to set delay in MILLISECOND [20..150]
*
* @param delay
*/
public void setDelay(int delay) {
if (delay >= 20 && delay <= 150)
this.mDelay = delay;
}
/**
* Call this to remove animation at any time
*/
public void removeAnimation() {
mHandler.removeCallbacks(mCharacterAdder);
animating = false;
setText(mPrintingText);
//typing removed
if (mTypeWriterListener != null)
mTypeWriterListener.onTypingRemoved(mPrintingText);
}
/**
* Set listener to receive typing effects
*
* @param typeWriterListener
*/
public void setTypeWriterListener(TypeWriterListener typeWriterListener) {
this.mTypeWriterListener = typeWriterListener;
}
}