本文简单记录由View绘制转为SurfaceView绘制的波形图问题.

上代码:

 public class VoiceLineView extends View {
private final int LINE = 0;
private final int RECT = 1; private int middleLineColor = Color.BLACK;
private int voiceLineColor = Color.BLACK;
private float middleLineHeight = 4;
private Paint paint;
private Paint paintVoicLine; /**
* 灵敏度
*/
private int sensibility = 4; private float maxVolume = 100; private float translateX = 0;
private boolean isSet = false; /**
* 振幅
*/
private float amplitude = 1;
/**
* 音量
*/
private float volume = 10;
private int fineness = 1;
private float targetVolume = 1; private long speedY = 50;
private float rectWidth = 25;
private float rectSpace = 5;
private float rectInitHeight = 4;
private List<Rect> rectList; private long lastTime = 0;
private int lineSpeed = 90; List<Path> paths = null; public VoiceLineView(Context context) {
super(context);
} public VoiceLineView(Context context, AttributeSet attrs) {
super(context, attrs);
initAtts(context, attrs);
} public VoiceLineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAtts(context, attrs);
} private void initAtts(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.voiceView);
mode = typedArray.getInt(R.styleable.voiceView_viewMode, 0);
voiceLineColor = typedArray.getColor(R.styleable.voiceView_voiceLine, Color.BLACK);
maxVolume = typedArray.getFloat(R.styleable.voiceView_maxVolume, 100);
sensibility = typedArray.getInt(R.styleable.voiceView_sensibility, 4);
if (mode == RECT) {
rectWidth = typedArray.getDimension(R.styleable.voiceView_rectWidth, 25);
rectSpace = typedArray.getDimension(R.styleable.voiceView_rectSpace, 5);
rectInitHeight = typedArray.getDimension(R.styleable.voiceView_rectInitHeight, 4);
} else {
middleLineColor = typedArray.getColor(R.styleable.voiceView_middleLine, Color.BLACK);
middleLineHeight = typedArray.getDimension(R.styleable.voiceView_middleLineHeight, 4);
lineSpeed = typedArray.getInt(R.styleable.voiceView_lineSpeed, 90);
fineness = typedArray.getInt(R.styleable.voiceView_fineness, 1);
paths = new ArrayList<>(20);
for (int i = 0; i < 20; i++) {
paths.add(new Path());
}
}
typedArray.recycle();
} @Override
protected void onDraw(Canvas canvas) { drawMiddleLine(canvas);
drawVoiceLine(canvas);     run();
} private void drawMiddleLine(Canvas canvas) {
if (paint == null) {
paint = new Paint();
paint.setColor(middleLineColor);
paint.setAntiAlias(true);
}
canvas.save();
canvas.drawRect(0, getHeight() / 2 - middleLineHeight / 2, getWidth(), getHeight() / 2 + middleLineHeight / 2, paint);
canvas.restore();
} private void drawVoiceLine(Canvas canvas) {
lineChange();
if (paintVoicLine == null) {
paintVoicLine = new Paint();
paintVoicLine.setColor(voiceLineColor);
paintVoicLine.setAntiAlias(true);
paintVoicLine.setStyle(Paint.Style.STROKE);
paintVoicLine.setStrokeWidth(2);
}
canvas.save();
int moveY = getHeight() / 2;
for (int i = 0; i < paths.size(); i++) {
paths.get(i).reset();
paths.get(i).moveTo(getWidth(), getHeight() / 2);
}
for (float i = getWidth() - 1; i >= 0; i -= fineness) {
amplitude = 4 * volume * i / getWidth() - 4 * volume * i * i / getWidth() / getWidth();
for (int n = 1; n <= paths.size(); n++) {
float sin = amplitude * (float) Math.sin((i - Math.pow(1.22, n)) * Math.PI / 180 - translateX);
paths.get(n - 1).lineTo(i, (2 * n * sin / paths.size() - 15 * sin / paths.size() + moveY));
}
}
for (int n = 0; n < paths.size(); n++) {
if (n == paths.size() - 1) {
paintVoicLine.setAlpha(255);
} else {
paintVoicLine.setAlpha(n * 130 / paths.size());
}
if (paintVoicLine.getAlpha() > 0) {
canvas.drawPath(paths.get(n), paintVoicLine);
}
}
canvas.restore();
} public void setVolume(int volume) {
if (volume > maxVolume * sensibility / 25) {
isSet = true;
this.targetVolume = getHeight() * volume / 2 / maxVolume;
}
} private void lineChange() {
if (lastTime == 0) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
if (System.currentTimeMillis() - lastTime > lineSpeed) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
return;
}
}
if (volume < targetVolume && isSet) {
volume += getHeight() / 30;
} else {
isSet = false;
if (volume <= 10) {
volume = 10;
} else {
if (volume < getHeight() / 30) {
volume -= getHeight() / 60;
} else {
volume -= getHeight() / 30;
}
}
}
} public void run() { invalidate(); } }

上面是View的canvas实现的波形图绘制,cpu占用率比较高,在20%左右,一边录音一边绘制,手机发烫是最明显的感觉了.

下面我们换成SurfaceView绘制的

public class VoiceLineSurfaceView extends SurfaceView implements Runnable,SurfaceView.Callback {
private final int LINE = 0;
private final int RECT = 1; private int middleLineColor = Color.BLACK;
private int voiceLineColor = Color.BLACK;
private float middleLineHeight = 4;
private Paint paint;
private Paint paintVoicLine;/**
* 灵敏度
*/
private int sensibility = 4; private float maxVolume = 100; private float translateX = 0;
private boolean isSet = false; /**
* 振幅
*/
private float amplitude = 1;
/**
* 音量
*/
private float volume = 10;
private int fineness = 1;
private float targetVolume = 1; private long speedY = 50;
private float rectWidth = 25;
private float rectSpace = 5;
private float rectInitHeight = 4;
private List<Rect> rectList; private long lastTime = 0;
private int lineSpeed = 90;
   private SurfaceHolder surfaceHolder;
List<Path> paths = null;
private boolean isWaveDrawing = false;
private boolean isMiddleLineDrawing = true;
private Thread thread;
private Canvas canvas; public VoiceLineSurfaceView(Context context) {
super(context);
} public VoiceLineSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initAtts(context, attrs);
} public VoiceLineSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAtts(context, attrs);
} private void initAtts(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.voiceView);
mode = typedArray.getInt(R.styleable.voiceView_viewMode, 0);
voiceLineColor = typedArray.getColor(R.styleable.voiceView_voiceLine, Color.BLACK);
maxVolume = typedArray.getFloat(R.styleable.voiceView_maxVolume, 100);
sensibility = typedArray.getInt(R.styleable.voiceView_sensibility, 4);
middleLineColor = typedArray.getColor(R.styleable.voiceView_middleLine, Color.BLACK);
middleLineHeight = typedArray.getDimension(R.styleable.voiceView_middleLineHeight, 4);
lineSpeed = typedArray.getInt(R.styleable.voiceView_lineSpeed, 90);
fineness = typedArray.getInt(R.styleable.voiceView_fineness, 1);
paths = new ArrayList<>(20);
for (int i = 0; i < 20; i++) {
paths.add(new Path());
}
typedArray.recycle();
    setZOrderOnTop(true);
    getHolder().setFormat(PixelFormat.TRANSLUENT);
    surfaceHolder = getHolder();
    surfaceHolder.addCallback(this);
}
private void drawMiddleLine(Canvas canvas) {
if (paint == null) {
paint = new Paint();
paint.setColor(middleLineColor);
paint.setAntiAlias(true);
}
canvas.save();
canvas.drawRect(0, getHeight() / 2 - middleLineHeight / 2, getWidth(), getHeight() / 2 + middleLineHeight / 2, paint);
canvas.restore();
} private void drawVoiceLine(Canvas canvas) {
lineChange();
if (paintVoicLine == null) {
paintVoicLine = new Paint();
paintVoicLine.setColor(voiceLineColor);
paintVoicLine.setAntiAlias(true);
paintVoicLine.setStyle(Paint.Style.STROKE);
paintVoicLine.setStrokeWidth(2);
}
canvas.save();
int moveY = getHeight() / 2;
for (int i = 0; i < paths.size(); i++) {
paths.get(i).reset();
paths.get(i).moveTo(getWidth(), getHeight() / 2);
}
for (float i = getWidth() - 1; i >= 0; i -= fineness) {
amplitude = 4 * volume * i / getWidth() - 4 * volume * i * i / getWidth() / getWidth();
for (int n = 1; n <= paths.size(); n++) {
float sin = amplitude * (float) Math.sin((i - Math.pow(1.22, n)) * Math.PI / 180 - translateX);
paths.get(n - 1).lineTo(i, (2 * n * sin / paths.size() - 15 * sin / paths.size() + moveY));
}
}
for (int n = 0; n < paths.size(); n++) {
if (n == paths.size() - 1) {
paintVoicLine.setAlpha(255);
} else {
paintVoicLine.setAlpha(n * 130 / paths.size());
}
if (paintVoicLine.getAlpha() > 0) {
canvas.drawPath(paths.get(n), paintVoicLine);
}
}
canvas.restore();
}
public void setVolume(int volume) {
if (volume > maxVolume * sensibility / 25) {
isSet = true;
this.targetVolume = getHeight() * volume / 2 / maxVolume;
}
} private void lineChange() {
if (lastTime == 0) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
if (System.currentTimeMillis() - lastTime > lineSpeed) {
lastTime = System.currentTimeMillis();
translateX += 1.5;
} else {
return;
}
}
if (volume < targetVolume && isSet) {
volume += getHeight() / 30;
} else {
isSet = false;
if (volume <= 10) {
volume = 10;
} else {
if (volume < getHeight() / 30) {
volume -= getHeight() / 60;
} else {
volume -= getHeight() / 30;
}
}
}
}
@Override
public void surfaceCreated(){
}
@Override
public void surfaceChanged(){
isMiddleLineDrawing = true;
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceDestroyed(){
isMiddleLineDrawing = false;
}
@Override
public void run() {
  while(isMiddleLineDrawing){
    canvas = surfaceHolder.lockCanvas();
    if(canvas==null){
    return;
    }
    canvas.drawColor(Color.TRANSLUENT,PorterDuff.Mode.CLEAR);
    if(isWaveDrawing){
    drawVoiceLine(canvas);
    }
    drawMiddleLine(canvas);
    surfaceHolder.unlockCanvasAndPost(canvas);
  }
} public void setDrawing(boolean drawing){
  isMiddleLineDrawing = true;
  isWaveDrawing = drawing;
} }

SurfaceView绘制录音波形图的更多相关文章

  1. C# NAudio录音和播放音频文件-实时绘制音频波形图(从音频流数据获取,而非设备获取)

    NAudio的录音和播放录音都有对应的类,我在使用Wav格式进行录音和播放录音时使用的类时WaveIn和WaveOut,这两个类是对功能的回调和一些事件触发. 在WaveIn和WaveOut之外还有对 ...

  2. C# NAudio录音和播放音频文件及实时绘制音频波形图(从音频流数据获取,而非设备获取)

    下午写了一篇关于NAudio的录音.播放和波形图的博客,不太满意,感觉写的太乱,又总结了下 NAudio是个相对成熟.开源的C#音频开发工具,它包含录音.播放录音.格式转换.混音调整等功能.本次介绍主 ...

  3. ios 绘制wav波形图

    最近研究了如何在iOS上绘制wav波形图.查了很多资料,都没能找到一个很完整的介绍,我这里总结一下一些经验. 首先需要了解wav的这3个重要指标:采样率.采样位数.声道数.下面以16KHz, 16Bi ...

  4. android SurfaceView绘制实现原理解析

    在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...

  5. Android -- SurfaceView绘制

    SurfaceView SurfaceView是View的一个特殊子类,它的目的是另外提供一个线程进行绘制操作. 步骤 1.用SurfaceView进行绘制,首先要创建一个类,继承 SurfaceVi ...

  6. Android: 利用SurfaceView绘制股票滑动直线解决延迟问题

    1.背景介绍 最近项目要绘制股票走势图,并绘制能够跟随手指滑动的指示线(Indicator)来精确查看股票价格和日期.如下图所示: 上图中的那条白色直线就是股票的指示线,用来跟随手指精确确定股票的时间 ...

  7. android:怎样用一天时间,写出“飞机大战”这种游戏!(无框架-SurfaceView绘制)

    序言作为一个android开发人员,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的. 体验地址:http://www.wandoujia. ...

  8. SurfaceView 绘制分形图

    之前一直做的是应用类,这次抽时间,参考网上资料实践了下SurfaceView.目标是在页面上画一个科赫曲线的分形图. 代码如下: package com.example.fredric.demo02; ...

  9. android SurfaceView绘制 重新学习--控制动画移动

    直接上demo,图是自己切的,将就用吧.点击左右两边分别向左右移动. public class MySurfaceView extends SurfaceView implements Callbac ...

随机推荐

  1. HDU 1108.最小公倍数-辗转相除法

    最小公倍数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 洛谷——P2121 拆地毯

    P2121 拆地毯 题目背景 还记得 NOIP 2011 提高组 Day1 中的铺地毯吗?时光飞逝,光阴荏苒,三年过去了.组织者精心准备的颁奖典礼早已结束,留下的则是被人们踩过的地毯.请你来解决类似于 ...

  3. Linux命令之passwd、chpasswd

    (1).命令passed passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inacti ...

  4. 持续获取password

    function GetPasswd { RunCounter=1 DB_PSSWD=getpassword while [ -z "${DB_PSSWD}" -a ${RunCo ...

  5. Windows命令远程执行工具Winexe

    Windows命令远程执行工具Winexe   在对Windows系统执行渗透测试中,通过各种方式可以获取目标主机的用户名和密码.这时,只要对方主机开启文件共享服务,就可以借助Winexe工具远程执行 ...

  6. 反编译apk 修改 合成

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 反编译apk帮助文档 准备工具 dex2jar(dex转换jar工具),下载地址: ht ...

  7. android viewpager fragment 优化 切换界面 延时加载

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 使用 碎片的 设置用户可见暗示visible hint  这个方法来做到. hint 是 ...

  8. 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ...

  9. python基础之递归,匿名,内置函数

    递归函数: 什么是递归函数? 函数递归调用:在调用一个函数的过程中,又直接或间接地调用了该函数本身. 递归必须要有两个明确的阶段: ①递推:一层一层递归调用下去,强调:每进入下一层问题规模减少 ②回溯 ...

  10. Oracle Linux logoOracle Linux

    http://www.oschina.net/p/oracle_enterprise_linux