First, take a look at the effect demo
Second, the realization principle:
This is actually not difficult to achieve, through a timer constantly called TextView
setText
on the line, in setText
the time to play the sound of typing.
The specific 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;
/** * Analog Typewriter effect * */public class Typetextview extends TextView {private context mcontext = null;
Private MediaPlayer mmediaplayer = null;
Private String mshowtextstring = null;
Private Timer Mtypetimer = null;
Private Ontypeviewlistener montypeviewlistener = null;
private static final int type_time_delay = 80; private int mtypetimedelay = Type_time_delay;
Typing interval public Typetextview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Inittypetextview (context);
Public Typetextview (context, AttributeSet attrs) {Super (context, attrs);
Inittypetextview (context);
Public Typetextview {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 Textutils.isempty (textstring) | | typeti
Medelay < 0) {return;
Post (new Runnable () {@Override public void run () {mshowtextstring = TextString;
Mtypetimedelay = Typetimedelay;
SetText ("");
Starttypetimer ();
if (null!= montypeviewlistener) {Montypeviewlistener.ontypestart ();
}
}
});
public void Stop () {Stoptypetimer ();
Stopaudio ();
private void Inittypetextview {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;
The class Typetimertask extends timertask{@Override public void Run () {post (new Runnable () {@Override public void Run () {if (GetText (). toString (). Length () < Mshowtextstring.length ()) {SetText (mshowtext
String.substring (0, GetText (). toString (). Length () + 1));
Startaudioplayer ();
Starttypetimer ();
}else{Stoptypetimer ();
if (null!= montypeviewlistener) {montypeviewlistener.ontypeover ();
}
}}
});
} public interface ontypeviewlistener{public void Ontypestart ();
public void Ontypeover (); }
}
Third, the use of instructions:
1, in the XML file definition:
<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 () {
@Override public
void Ontypestart () {
print ("Ontypestart");
}
@Override public
void Ontypeover () {
print ("Ontypeover");
}
);
3. Call Start Method:
Mtypetextview.start (Test_data);
Iv. Summary
The above is the Android custom view to achieve the full contents of the typewriter effect, interested in the quick hands-on practice, I hope this article on the content of the development of Android can help, if you have questions can be exchanged message.