仿微信的语音点击播放时的语音图标动画效果

仿微信的语音点击播放时的语音图标动画效果

效果:

可能需要的图片:

需要定义的实例变量:

//语音动画控制器
Timer mTimer=null;
//语音动画控制任务
TimerTask mTimerTask=null;
//记录语音动画图片
int index=1;
AudioAnimationHandler audioAnimationHandler=null;

需要定义的函数:

代码:

/**
	 * 播放语音图标动画
	 */
	private void playAudioAnimation(final ImageView imageView) {
		//定时器检查播放状态   
		stopTimer();
		mTimer=new Timer();
		//将要关闭的语音图片归位
		if(audioAnimationHandler!=null)
		{
	    	Message msg=new Message();
	    	msg.what=3;
	    	audioAnimationHandler.sendMessage(msg);
		}
    	
        audioAnimationHandler=new AudioAnimationHandler(imageView);
        mTimerTask = new TimerTask() { 
        	public boolean hasPlayed=false;
            @Override    
            public void run() {     
                if(mMediaPlayer.isPlaying()) {  
                	hasPlayed=true;
                	index=(index+1)%3;
                	Message msg=new Message();
                	msg.what=index;
                	audioAnimationHandler.sendMessage(msg);
                }else
                {
                	//当播放完时
                	Message msg=new Message();
                	msg.what=3;
                	audioAnimationHandler.sendMessage(msg);
                	//播放完毕时需要关闭Timer等
                	if(hasPlayed)
                	{
                		stopTimer();
                	}
                }
            }
        };
        //调用频率为500毫秒一次
        mTimer.schedule(mTimerTask, 0, 500);
	}
	class AudioAnimationHandler extends Handler
	{
		ImageView imageView;
		//判断是左对话框还是右对话框
		boolean isleft;
		public AudioAnimationHandler(ImageView imageView)
		{
			this.imageView=imageView;
			//判断是左对话框还是右对话框 我这里是在前面设置ScaleType来表示的
			isleft=imageView.getScaleType()==ScaleType.FIT_START?true:false;
		}
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			//根据msg.what来替换图片,达到动画效果
        	switch (msg.what) {
    			case 0 :
    				imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f1:R.drawable.chatto_voice_playing_f1);
    				break;
    			case 1 :
    				imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f2:R.drawable.chatto_voice_playing_f2);
    				break;
    			case 2 :
    				imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f3:R.drawable.chatto_voice_playing_f3);
    				break;
    			default :
    				imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f3:R.drawable.chatto_voice_playing_f3);
    				break;
    		}
		}
		
	}
	/**
	 * 停止
	 */
	 private void stopTimer(){  
	        if (mTimer != null) {  
	            mTimer.cancel();  
	            mTimer = null;  
	        }  
	  
	        if (mTimerTask != null) {  
	            mTimerTask.cancel();  
	            mTimerTask = null;  
	        }   

	    } 


你可能感兴趣的:(android开发)