Android模仿打字机效果的自定义View实现

龙旋

共 4242字,需浏览 9分钟

 ·

2022-08-03 07:01

在做splash界面的时候,需要做类似于打字机打字的效果,字一个一个地蹦出来,显示每一个字都带有打字的声音。


效果图:



这个其实不难实现,通过一个定时器不断调用TextView的setText就行了,在setText的时候播放打字的音效。具体代码如下:

/** * 模拟打字机效果 *  * */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 );


源码地址:

https://github.com/zmywly8866/TypeTextView


浏览 70
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报