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

来源:互联网 发布:mac上如何卸载软件 编辑:程序博客网 时间:2024/06/05 03:22

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

效果:

可能需要的图片:

需要定义的实例变量:

[java] view plain copy
  1. //语音动画控制器  
  2. Timer mTimer=null;  
  3. //语音动画控制任务  
  4. TimerTask mTimerTask=null;  
  5. //记录语音动画图片  
  6. int index=1;  
  7. AudioAnimationHandler audioAnimationHandler=null;  

需要定义的函数:

代码:

[java] view plain copy
  1. /** 
  2.      * 播放语音图标动画 
  3.      */  
  4.     private void playAudioAnimation(final ImageView imageView) {  
  5.         //定时器检查播放状态     
  6.         stopTimer();  
  7.         mTimer=new Timer();  
  8.         //将要关闭的语音图片归位  
  9.         if(audioAnimationHandler!=null)  
  10.         {  
  11.             Message msg=new Message();  
  12.             msg.what=3;  
  13.             audioAnimationHandler.sendMessage(msg);  
  14.         }  
  15.           
  16.         audioAnimationHandler=new AudioAnimationHandler(imageView);  
  17.         mTimerTask = new TimerTask() {   
  18.             public boolean hasPlayed=false;  
  19.             @Override      
  20.             public void run() {       
  21.                 if(mMediaPlayer.isPlaying()) {    
  22.                     hasPlayed=true;  
  23.                     index=(index+1)%3;  
  24.                     Message msg=new Message();  
  25.                     msg.what=index;  
  26.                     audioAnimationHandler.sendMessage(msg);  
  27.                 }else  
  28.                 {  
  29.                     //当播放完时  
  30.                     Message msg=new Message();  
  31.                     msg.what=3;  
  32.                     audioAnimationHandler.sendMessage(msg);  
  33.                     //播放完毕时需要关闭Timer等  
  34.                     if(hasPlayed)  
  35.                     {  
  36.                         stopTimer();  
  37.                     }  
  38.                 }  
  39.             }  
  40.         };  
  41.         //调用频率为500毫秒一次  
  42.         mTimer.schedule(mTimerTask, 0500);  
  43.     }  
  44.     class AudioAnimationHandler extends Handler  
  45.     {  
  46.         ImageView imageView;  
  47.         //判断是左对话框还是右对话框  
  48.         boolean isleft;  
  49.         public AudioAnimationHandler(ImageView imageView)  
  50.         {  
  51.             this.imageView=imageView;  
  52.             //判断是左对话框还是右对话框 我这里是在前面设置ScaleType来表示的  
  53.             isleft=imageView.getScaleType()==ScaleType.FIT_START?true:false;  
  54.         }  
  55.         @Override  
  56.         public void handleMessage(Message msg) {  
  57.             super.handleMessage(msg);  
  58.             //根据msg.what来替换图片,达到动画效果  
  59.             switch (msg.what) {  
  60.                 case 0 :  
  61.                     imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f1:R.drawable.chatto_voice_playing_f1);  
  62.                     break;  
  63.                 case 1 :  
  64.                     imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f2:R.drawable.chatto_voice_playing_f2);  
  65.                     break;  
  66.                 case 2 :  
  67.                     imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f3:R.drawable.chatto_voice_playing_f3);  
  68.                     break;  
  69.                 default :  
  70.                     imageView.setImageResource(isleft?R.drawable.chatfrom_voice_playing_f3:R.drawable.chatto_voice_playing_f3);  
  71.                     break;  
  72.             }  
  73.         }  
  74.           
  75.     }  
  76.     /** 
  77.      * 停止 
  78.      */  
  79.      private void stopTimer(){    
  80.             if (mTimer != null) {    
  81.                 mTimer.cancel();    
  82.                 mTimer = null;    
  83.             }    
  84.         
  85.             if (mTimerTask != null) {    
  86.                 mTimerTask.cancel();    
  87.                 mTimerTask = null;    
  88.             }     
  89.   
  90.         }   
0 0