Android imitates the custom View implementation of the typewriter effect, and android typewriter
I. Preface
During the splash interface, words need to be typed in a manner similar to typewriters. Each word is displayed with a typing sound.
Ii. effect demonstration
In this example, the user-defined View demonstrates the following effect (PS: I never know how to record gif animation on Android, and simply run Genymotion Android simulator on PC, then you can use LICEcap to screen recording .).
Iii. Implementation principle:
This is actually not difficult to implement. You can use a timer to continuously call the setText of TextView and play the sound effects of typing during setText. The Code is as follows:
Import java. util. timer; import java. util. timerTask; import android. content. context; import android. media. mediaPlayer; import android. text. textUtils; import android. util. attributeSet; import android. widget. textView; import com. uperone. typetextview. r;/*** simulate typewriter effect ***/public class TypeTextView extends TextView {private Context mContext = null; private MediaPlayer mMediaPlayer = null; private String mShowTex TString = null; private Timer mTypeTimer = null; private OnTypeViewListener mOnTypeViewListener = null; private static final int TYPE_TIME_DELAY = 80; private int duration = TYPE_TIME_DELAY; // The interval between typing and public TypeTextView, attributeSet attrs, int defStyle) {super (context, attrs, defStyle); initTypeTextView (context);} public TypeTextView (Context context, AttributeSet attrs) {super (con Text, attrs); initTypeTextView (context);} public TypeTextView (Context context) {super (context); initTypeTextView (context);} public void setOnTypeViewListener (OnTypeViewListener onTypeViewListener) {mOnTypeViewListener = onTypeViewListener;} public void start (final String textString) {start (textString, TYPE_TIME_DELAY);} public void start (final String textString, final int typeTimeDelay) {if (Te XtUtils. isEmpty (textString) | typeTimeDelay <0) {return;} post (new Runnable () {@ Overridepublic void run () {mShowTextString = textString; mTypeTimeDelay = typeTimeDelay; setText (""); startTypeTimer (); if (null! = MOnTypeViewListener) {mOnTypeViewListener. onTypeStart () ;}}) ;}public void stop () {stopTypeTimer (); stopAudio () ;}private void initTypeTextView (Context context) {mContext = context ;} private void startTypeTimer () {stopTypeTimer (); mTypeTimer = new Timer (); mTypeTimer. schedule (new TypeTimerTask (), mTypeTimeDelay);} private void stopTypeTimer () {if (null! = MTypeTimer) {mTypeTimer. cancel (); mTypeTimer = null;} private void startAudioPlayer () {stopAudio (); playAudio (R. raw. type_in);} private void playAudio (int audioResId) {try {stopAudio (); mMediaPlayer = MediaPlayer. create (mContext, audioResId); mMediaPlayer. start ();} catch (Exception e) {e. printStackTrace () ;}} private void stopAudio () {if (mMediaPlayer! = Null & mMediaPlayer. isPlaying () {mMediaPlayer. stop (); mMediaPlayer. release (); mMediaPlayer = null;} class TypeTimerTask extends TimerTask {@ Overridepublic void run () {post (new Runnable () {@ Overridepublic void run () {if (getText (). toString (). length () <mShowTextString. length () {setText (mShowTextString. substring (0, getText (). toString (). length () + 1); startAudioPlayer (); startTypeTi Mer ();} else {stopTypeTimer (); if (null! = MOnTypeViewListener) {mOnTypeViewListener. onTypeOver () ;}}}) ;}} public interface OnTypeViewListener {public void onTypeStart (); public void onTypeOver ();}}
Iv. Instructions for use:
1. Define in the xml file:
<com.uperone.typetext.view.TypeTextView android:id="@+id/typeTxtId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" />
2. instantiate in code:
mTypeTextView = ( TypeTextView )findViewById(R.id.typeTxtId);mTypeTextView.setOnTypeViewListener( new OnTypeViewListener( ) {@Overridepublic void onTypeStart() {print( "onTypeStart" );}@Overridepublic void onTypeOver() {print( "onTypeOver" );}});
3. Call the start method:
mTypeTextView.start( TEST_DATA );
V,
I uploaded this demo to github. You can download it here and modify it as needed. TypeTextView