Android实现微信聊天语言点击喇叭动画效果

2020-11-05 14:45:03 浏览数 (1)

微信聊天界面点击语音时,喇叭的动画效果,下面上代码,该类是控制背景变化的工具类,不涉及语音录制和播放。

图片资源:

代码语言:javascript复制
package xxx;

import android.os.Handler;
import android.widget.ImageView;

import xxx.R;

import java.util.Timer;
import java.util.TimerTask;

public class VoicePlayingBgUtil {

  private Handler handler;

  private ImageView imageView;

  private ImageView lastImageView;

  private Timer timer = new Timer();
  private TimerTask timerTask;

  private int i;

  private int modelType = 1;//类型

  private int[] leftVoiceBg = new int[] { R.drawable.gray1, R.drawable.gray2, R.drawable.gray3 };
  private int[] rightVoiceBg = new int[] { R.drawable.green1, R.drawable.green2, R.drawable.green3 };
  private int[] collectVoiceBg = new int[] { R.drawable.collect_voice_1, R.drawable.collect_voice_2, R.drawable.collect_voice_3 };

  public VoicePlayingBgUtil(Handler handler) {
    super();
    this.handler = handler;
  }

  public void voicePlay() {
    if (imageView == null) {
      return;
    }
    i = 0;
    timerTask = new TimerTask() {

      @Override
      public void run() {
        if (imageView != null) {
          if (modelType == 1) {
            changeBg(leftVoiceBg[i % 3], false);
          }else if(modelType==2){
            changeBg(rightVoiceBg[i % 3], false);
          }else if(modelType==3){
            changeBg(collectVoiceBg[i % 3], false);
          }
        }
        else {
          return;
        }
        i  ;
      }
    };
    timer.schedule(timerTask, 0, 500);
  }

  public void stopPlay() {
    lastImageView = imageView;
    if (lastImageView != null) {
      switch (modelType) {
        case 1:
          changeBg(R.drawable.gray3, true);
          break;
        case 2:
          changeBg(R.drawable.green3, true);
          break;
        case 3:
          changeBg(R.drawable.collect_voice_3, true);
        default:
          changeBg(R.drawable.gray3, true);
          break;
      }
      if (timerTask != null) {
        timerTask.cancel();
      }
    }
  }

  private void changeBg(final int id, final boolean isStop) {
    handler.post(new Runnable() {
      @Override
      public void run() {
        if (isStop) {
          lastImageView.setImageResource(id);
        }
        else {
          imageView.setImageResource(id);

        }
      }
    });
  }

  public void setImageView(ImageView imageView) {
    this.imageView = imageView;
  }

  public void setModelType(int modelType) {
    this.modelType = modelType;
  }

}

调用方法

代码语言:javascript复制
 playBgUtil = new VoicePlayingBgUtil(handler);
 playBgUtil.stopPlay();
 playBgUtil.voicePlay();

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞