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

Android语音声波控件 Android条形波控件,第1张

概述许久不来,冒个泡,发一个刚做的声音波动的View吧:代码不多,没什么技术含量,权当给您省时间了,直接复制粘贴就能用,直接上代码:

许久不来,冒个泡,发一个刚做的声音波动的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,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,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,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语音声波控件 Android条形波控件全部内容,希望文章能够帮你解决Android语音声波控件 Android条形波控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存