Skip to content

Commit d40bd81

Browse files
committed
Add Library content
1 parent c021c86 commit d40bd81

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed
Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,107 @@
11
package com.nitish.typewriterview;
22

3-
public class TypeWriterView {
3+
import android.content.Context;
4+
import android.os.Handler;
5+
import android.util.AttributeSet;
6+
7+
import androidx.appcompat.widget.AppCompatTextView;
8+
9+
public class TypeWriterView extends AppCompatTextView {
10+
private CharSequence mText;
11+
private int mIndex;
12+
private long mDelay = 40; //Default delay in ms
13+
private Boolean isAnimationRunning = false;
14+
private OnAnimationChangeListener mAnimationChangeListener;
15+
private Boolean avoidTextOverflowAtEdge = true;
16+
17+
public TypeWriterView(Context context) {
18+
super(context);
19+
}
20+
21+
public TypeWriterView(Context context, AttributeSet attrs) {
22+
super(context, attrs);
23+
}
24+
25+
private Handler mHandler = new Handler();
26+
private Runnable characterAdder = new Runnable() {
27+
@Override
28+
public void run() {
29+
setText(mText.subSequence(0, mIndex++));
30+
if(mIndex <= mText.length()) {
31+
mHandler.postDelayed(characterAdder, mDelay);
32+
isAnimationRunning = true;
33+
}else {
34+
isAnimationRunning = false;
35+
if (mAnimationChangeListener != null) mAnimationChangeListener.onAnimationEnd();
36+
}
37+
}
38+
};
39+
40+
public void animateText(CharSequence text) {
41+
mText = generateText(text.toString());
42+
mIndex = 0;
43+
44+
setText("");
45+
mHandler.removeCallbacks(characterAdder);
46+
mHandler.postDelayed(characterAdder, mDelay);
47+
}
48+
49+
public void stopAnimation(){
50+
if (isAnimationRunning) {
51+
isAnimationRunning = false;
52+
mHandler.removeCallbacks(characterAdder);
53+
setText(mText);
54+
if (mAnimationChangeListener != null) mAnimationChangeListener.onAnimationEnd();
55+
}
56+
}
57+
58+
public boolean isAnimationRunning(){
59+
return isAnimationRunning;
60+
}
61+
62+
public boolean isTextInitialised() {
63+
return mText != null;
64+
}
65+
66+
//To Explicitly Change the Delay
67+
public void setCharacterDelay(long millis) {
68+
mDelay = millis;
69+
}
70+
71+
public String generateText(String mText){
72+
if (avoidTextOverflowAtEdge){
73+
return generateFormattedSequence(mText);
74+
}
75+
return mText;
76+
}
77+
78+
public String generateFormattedSequence(String mText){
79+
String[] words = mText.split(" ");
80+
int viewWidth = getMeasuredWidth();
81+
StringBuilder finalSequence = new StringBuilder();
82+
for (String word : words){
83+
String temp = finalSequence.substring(Math.max(finalSequence.lastIndexOf("\n"), 0)) + " " + word;
84+
float textWidth = getPaint().measureText(temp);
85+
if (textWidth >= viewWidth)
86+
finalSequence.append("\n").append(word);
87+
else if (finalSequence.length() <= 0)
88+
finalSequence.append(word);
89+
else
90+
finalSequence.append(" ").append(word);
91+
}
92+
return finalSequence.toString();
93+
}
94+
95+
//Explicitly turnoff "avoidTextOverflowAtEdge" to avoid weird text formatting in few cases (when view size is dynamic).
96+
public void avoidTextOverflowAtEdge(boolean avoidTextOverflowAtEdge) {
97+
this.avoidTextOverflowAtEdge = avoidTextOverflowAtEdge;
98+
}
99+
100+
public interface OnAnimationChangeListener {
101+
void onAnimationEnd();
102+
}
103+
104+
public void setOnAnimationChangeListener(OnAnimationChangeListener onAnimationChangeListener) {
105+
mAnimationChangeListener = onAnimationChangeListener;
106+
}
4107
}

0 commit comments

Comments
 (0)