Android语音声波控件 Android条形波控件
程序员文章站
2023-12-19 16:46:40
许久不来 , 冒个泡 , 发一个刚做的声音波动的view吧 :
代码不多 , 没什么技术含量 , 权当给您省时间了 , 直接复制粘贴就能用 , 直接上代码:
so...
许久不来 , 冒个泡 , 发一个刚做的声音波动的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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。