First, preface
in the splash interface, you need to do similar to typewriter typing effect, the word jumps out one by one, showing each word with a typing sound.
Second, the effect demonstration
The demo of this example custom view is as follows (PS: never know how to animate GIF format on Android, simply run the Genymotion android emulator on the PC and then use the Licecap to record the screen. )。
Three, the realization principle:
This is actually not difficult to achieve, through a timer constantly call TextView SetText on the line, in the SetText when playing 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;/** * Analogue Typewriter effect * * */public class Typetextview extends TextView {private Context Mcontext = nul L;private MediaPlayer mmediaplayer = null;private String mshowtextstring = null;private Timer Mtypetimer = null;private on Typeviewlistener Montypeviewlistener = null;private static final int type_time_delay = 80;private int mtypetimedelay = TYP E_time_delay; Typing interval public Typetextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Inittypetextview (context);} Public Typetextview (context context, AttributeSet Attrs) {Super (context, 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 (Textutils.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 (), Mtype Timedelay);} private void Stoptypetimer () {if (null! = Mtypetimer) {mtypetimer.cancel (); mtypetimer = null;}} private void Startaudioplayer () {Stopaudio ();p Layaudio (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 (); Starttypetimer ();} Else{stoptypetimer (); if (null! = Montypeviewlistener) {montypeviewlistener.ontypeover ();}}});}} public interface ontypeviewlistener{public void Ontypestart ();p ublic void Ontypeover ();}
Iv. Instructions for use:
1. defined 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);
Five
I uploaded this demo to GitHub and can download it here and change it. Typetextview
Android's custom view implementation that mimics the typewriter effect