-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c021c86
commit d40bd81
Showing
1 changed file
with
104 additions
and
1 deletion.
There are no files selected for viewing
105 changes: 104 additions & 1 deletion
105
TypeWriterView/src/main/java/com/nitish/typewriterview/TypeWriterView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,107 @@ | ||
package com.nitish.typewriterview; | ||
|
||
public class TypeWriterView { | ||
import android.content.Context; | ||
import android.os.Handler; | ||
import android.util.AttributeSet; | ||
|
||
import androidx.appcompat.widget.AppCompatTextView; | ||
|
||
public class TypeWriterView extends AppCompatTextView { | ||
private CharSequence mText; | ||
private int mIndex; | ||
private long mDelay = 40; //Default delay in ms | ||
private Boolean isAnimationRunning = false; | ||
private OnAnimationChangeListener mAnimationChangeListener; | ||
private Boolean avoidTextOverflowAtEdge = true; | ||
|
||
public TypeWriterView(Context context) { | ||
super(context); | ||
} | ||
|
||
public TypeWriterView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
private Handler mHandler = new Handler(); | ||
private Runnable characterAdder = new Runnable() { | ||
@Override | ||
public void run() { | ||
setText(mText.subSequence(0, mIndex++)); | ||
if(mIndex <= mText.length()) { | ||
mHandler.postDelayed(characterAdder, mDelay); | ||
isAnimationRunning = true; | ||
}else { | ||
isAnimationRunning = false; | ||
if (mAnimationChangeListener != null) mAnimationChangeListener.onAnimationEnd(); | ||
} | ||
} | ||
}; | ||
|
||
public void animateText(CharSequence text) { | ||
mText = generateText(text.toString()); | ||
mIndex = 0; | ||
|
||
setText(""); | ||
mHandler.removeCallbacks(characterAdder); | ||
mHandler.postDelayed(characterAdder, mDelay); | ||
} | ||
|
||
public void stopAnimation(){ | ||
if (isAnimationRunning) { | ||
isAnimationRunning = false; | ||
mHandler.removeCallbacks(characterAdder); | ||
setText(mText); | ||
if (mAnimationChangeListener != null) mAnimationChangeListener.onAnimationEnd(); | ||
} | ||
} | ||
|
||
public boolean isAnimationRunning(){ | ||
return isAnimationRunning; | ||
} | ||
|
||
public boolean isTextInitialised() { | ||
return mText != null; | ||
} | ||
|
||
//To Explicitly Change the Delay | ||
public void setCharacterDelay(long millis) { | ||
mDelay = millis; | ||
} | ||
|
||
public String generateText(String mText){ | ||
if (avoidTextOverflowAtEdge){ | ||
return generateFormattedSequence(mText); | ||
} | ||
return mText; | ||
} | ||
|
||
public String generateFormattedSequence(String mText){ | ||
String[] words = mText.split(" "); | ||
int viewWidth = getMeasuredWidth(); | ||
StringBuilder finalSequence = new StringBuilder(); | ||
for (String word : words){ | ||
String temp = finalSequence.substring(Math.max(finalSequence.lastIndexOf("\n"), 0)) + " " + word; | ||
float textWidth = getPaint().measureText(temp); | ||
if (textWidth >= viewWidth) | ||
finalSequence.append("\n").append(word); | ||
else if (finalSequence.length() <= 0) | ||
finalSequence.append(word); | ||
else | ||
finalSequence.append(" ").append(word); | ||
} | ||
return finalSequence.toString(); | ||
} | ||
|
||
//Explicitly turnoff "avoidTextOverflowAtEdge" to avoid weird text formatting in few cases (when view size is dynamic). | ||
public void avoidTextOverflowAtEdge(boolean avoidTextOverflowAtEdge) { | ||
this.avoidTextOverflowAtEdge = avoidTextOverflowAtEdge; | ||
} | ||
|
||
public interface OnAnimationChangeListener { | ||
void onAnimationEnd(); | ||
} | ||
|
||
public void setOnAnimationChangeListener(OnAnimationChangeListener onAnimationChangeListener) { | ||
mAnimationChangeListener = onAnimationChangeListener; | ||
} | ||
} |