Android语音声波控件 Android条形波控件

所属分类: 软件编程 / Android 阅读数: 52
收藏 0 赞 0 分享

许久不来 , 冒个泡 , 发一个刚做的声音波动的View吧 :

代码不多 , 没什么技术含量 , 权当给您省时间了 , 直接复制粘贴就能用 , 直接上代码:

SoundWavesView

/**
 * 语音通话的声波控件
 * Created by Mr.LongFace on 2017/9/16.
 */
public class SoundWavesView extends View {

  private int mMini; // 最短值
  private int mMax; // 最大值
  private int mLineWidth; // 每条声波的宽度
  private int mSoundNum = 5; // 声波的数量
  private int mSpac; // 每条声波的中点
  private int mWidth , mHeight; // 控件宽高
  private boolean isRun = false;

  private Paint mPaint;
  private RectF mRectF;
  private List<SoundLine> mSoundList = new ArrayList<>();
  private Handler mHandler = new Handler();
  private Runnable mInvalidateRun = new Runnable() {
    @Override
    public void run() {
      postInvalidate();
    }
  };

  public SoundWavesView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(getResources().getColor(R.color.color_red));
    mPaint.setStyle(Paint.Style.FILL);
    mRectF = new RectF();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (widthMeasureSpec > 0 && heightMeasureSpec > 0) {
      initParam();
    }
  }

  private void initParam() {
    mWidth = getWidth();
    mHeight = getHeight();
    mMini = (int) (mHeight * 0.3f);
    mMax = mHeight;
    initLines();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    for (int i = 0; i < mSoundNum; i++) {
      SoundLine sound = mSoundList.get(i);
      mRectF.left = sound.left;
      mRectF.right = sound.right;
      mRectF.top = sound.top;
      mRectF.bottom = sound.bottom;
      canvas.drawRoundRect(mRectF , mLineWidth / 2 , mLineWidth / 2 , mPaint);
    }
    if (isRun) {
      mHandler.postDelayed(mInvalidateRun, 10);
    }
  }

  @Override
  protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
    super.onVisibilityChanged(changedView, visibility);
    if (isRun) {
      if (visibility == VISIBLE) {
        if (mWidth == 0) {
          initParam();
        }
        if (mSoundList != null && mSoundList.size() > 0) {
          for (SoundLine soundLine : mSoundList) {
            soundLine.start();
          }
        }
      }else{
        if (mSoundList != null && mSoundList.size() > 0) {
          for (SoundLine soundLine : mSoundList) {
            soundLine.stop();
          }
        }
      }
    }
  }

  public void start() {
    if (!isRun) {
      isRun = true;
      for (SoundLine sound : mSoundList) {
        sound.start();
      }
      postInvalidate();
    }
  }

  public void stop(){
    if (isRun) {
      isRun = false;
      for (SoundLine sound : mSoundList) {
        sound.stop();
      }
    }
  }

  private void initLines() {
    mLineWidth = (int) (mWidth / mSoundNum * 0.7f);
    mSpac = mWidth / (mSoundNum - 1);
    mSoundList.clear();
    chaos();
  }

  /**
   * 生成凌乱的
   */
  private void chaos() {
    for (int i = 0; i < mSoundNum; i++) {
      int left = i * mSpac - mLineWidth / 2;
      int right = i * mSpac + mLineWidth / 2;
      SoundLine s = new SoundLine(left , right , 0 , mHeight);
      s.setMode(SoundLine.SPEED_RAN);
      s.setBorder(mMini , mMax);
      mSoundList.add(s);
    }
  }

  /**
   * 生成波浪的
   */
  private void wave(){
    // TODO 防止UI抽风
  }

  /**
   * 生成有序的
   */
  private void order(){
    // TODO 防止UI抽风
  }
}

SoundLine

/**
 * 语音音频波纹的单个音波属性
 * Created by Mr.LongFace on 2017/9/16.
 */
public class SoundLine implements ValueAnimator.AnimatorUpdateListener{

  // 低 中 高 随机 4挡
  public static final int SPEED_LOW = 500;
  public static final int SPEED_MID = 200;
  public static final int SPEED_HEI = 0;
  public static final int SPEED_RAN = 0;

  private Random mRandom;
  private ValueAnimator mAnim;

  public int left , right , top , bottom;
  private int min , max;

  public SoundLine(int left , int right , int top , int bottom){
    this.left = left;
    this.right = right;
    this.top = top;
    this.bottom = bottom;
    mRandom = new Random();
    initAnim();
  }

  private void initAnim() {
    mAnim = ValueAnimator.ofFloat(0.0f , 1.0f);
    setMode(SPEED_MID);
    mAnim.setRepeatCount(-1);
    mAnim.setRepeatMode(ValueAnimator.REVERSE);
    mAnim.addUpdateListener(this);
  }

  public void setMode(int mode){
    if (mode == SPEED_RAN) {
      mode = mRandom.nextInt(400);
    }
    mAnim.setDuration(300 + mode);
  }

  public void start(){
    if (mAnim.isRunning()){
      mAnim.end();
    }
    mAnim.start();
  }

  @Override
  public void onAnimationUpdate(ValueAnimator valueAnimator) {
    float f = (float) valueAnimator.getAnimatedValue();
    top = (int) (f * (max - min) / 2);
    bottom = max - top;
  }

  public void setBorder(int min, int max) {
    this.min = min;
    this.max = max;
  }

  public void stop() {
    mAnim.end();
    mAnim.cancel();
  }
}

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多