Android自定义View实现打字机效果
程序员文章站
2024-03-06 20:20:08
一、先来看看效果演示
二、实现原理:
这个其实不难实现,通过一个定时器不断调用textview的settext就行了,在settext的时候播放打字的音效。...
一、先来看看效果演示
二、实现原理:
这个其实不难实现,通过一个定时器不断调用textview
的settext
就行了,在settext
的时候播放打字的音效。
具体代码如下:
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; /** * 模拟打字机效果 * * */ 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; // 打字间隔 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( ) { @override public 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{ @override public void run() { post(new runnable( ) { @override public 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( ); public void ontypeover( ); } }
三、使用说明:
1、在xml文件中定义:
<com.uperone.typetext.view.typetextview android:id="@+id/typetxtid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centervertical="true" />
2、在代码中实例化:
mtypetextview = ( typetextview )findviewbyid(r.id.typetxtid); mtypetextview.setontypeviewlistener( new ontypeviewlistener( ) { @override public void ontypestart() { print( "ontypestart" ); } @override public void ontypeover() { print( "ontypeover" ); } });
3、调用start方法:
mtypetextview.start( test_data );
四、总结
以上就是android自定义view实现打字机效果的全部内容,感兴趣的快快自己动手实践起来,希望本文的内容对大家开发android的时候能有所帮助,如果有疑问可以留言交流。