Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android提高21篇之十一:模擬信號示波器

Android提高21篇之十一:模擬信號示波器

編輯:關於android開發

       上次簡單地介紹了AudioRecord和AudioTrack的使用,這次就結合SurfaceView實現一個Android版的手機模擬信號示波器。最近物聯網炒得很火,作為手機軟件開發者,如何在不修改手機硬件電路的前提下實現與第三方傳感器結合呢?麥克風就是一個很好的ADC接口,通過麥克風與第三方傳感器結合,再在軟件裡對模擬信號做相應的處理,就可以提供更豐富的傳感化應用。

       先來看看本文程序運行的效果圖(屏幕錄像速度較慢,真機實際運行起來會更加流暢):

       本文程序使用8000hz的采樣率,對X軸方向繪圖的實時性要求較高,如果不降低X軸的分辨率,程序的實時性較差,因此程序對X軸數據縮小區間為8倍~16倍。由於采用16位采樣,因此Y軸數據的高度相對於手機屏幕來說也偏大,程序也對Y軸數據做縮小,區間為1倍~10倍。在SurfaceView的OnTouchListener方法裡加入了波形基線的位置調節,直接在SurfaceView控件上觸摸即可控制整體波形偏上或偏下顯示。

       main.xml源碼如下:

XML/HTML代碼
  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.         android:orientation="vertical" android:layout_width="fill_parent"  
  3.         android:layout_height="fill_parent">  
  4.         <linearlayout android:id="@+id/LinearLayout01"   
  5.                 android:layout_height="wrap_content" android:layout_width="fill_parent"  
  6.                 android:orientation="horizontal">  
  7.                 <button android:layout_height="wrap_content" android:id="@+id/btnStart"   
  8.                         android:text="開始" android:layout_width="80dip">  
  9.                 <button android:layout_height="wrap_content" android:text="停止"   
  10.                         android:id="@+id/btnExit" android:layout_width="80dip">  
  11.                 <zoomcontrols android:layout_width="wrap_content"   
  12.                         android:layout_height="wrap_content" android:id="@+id/zctlX">  
  13.                 <zoomcontrols android:layout_width="wrap_content"   
  14.                         android:layout_height="wrap_content" android:id="@+id/zctlY">  
  15.           
  16.         <surfaceview android:id="@+id/SurfaceView01"   
  17.                 android:layout_height="fill_parent" android:layout_width="fill_parent">  

       ClsOscilloscope.java是實現示波器的類庫,包含AudioRecord操作線程和SurfaceView繪圖線程的實現,兩個線程同步操作,代碼如下:

Java代碼
  1. package com.testOscilloscope;  
  2. import java.util.ArrayList;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Rect;  
  7. import android.media.AudioRecord;  
  8. import android.view.SurfaceView;  
  9. public class ClsOscilloscope {  
  10.         private ArrayList inBuf = new ArrayList();  
  11.         private boolean isRecording = false;// 線程控制標記  
  12.         /** 
  13.          * X軸縮小的比例 
  14.          */  
  15.         public int rateX = 4;  
  16.         /** 
  17.          * Y軸縮小的比例 
  18.          */  
  19.         public int rateY = 4;  
  20.         /** 
  21.          * Y軸基線 
  22.          */  
  23.         public int baseLine = 0;  
  24.         /** 
  25.          * 初始化 
  26.          */  
  27.         public void initOscilloscope(int rateX, int rateY, int baseLine) {  
  28.                 this.rateX = rateX;  
  29.                 this.rateY = rateY;  
  30.                 this.baseLine = baseLine;  
  31.         }  
  32.         /** 
  33.          * 開始 
  34.          *  
  35.          * @param recBufSize 
  36.          *            AudioRecord的MinBufferSize 
  37.          */  
  38.         public void Start(AudioRecord audioRecord, int recBufSize, SurfaceView sfv,  
  39.                         Paint mPaint) {  
  40.                 isRecording = true;  
  41.                 new RecordThread(audioRecord, recBufSize).start();// 開始錄制線程  
  42.                 new DrawThread(sfv, mPaint).start();// 開始繪制線程  
  43.         }  
  44.         /** 
  45.          * 停止 
  46.          */  
  47.         public void Stop() {  
  48.                 isRecording = false;  
  49.                 inBuf.clear();// 清除  
  50.         }  
  51.         /** 
  52.          * 負責從MIC保存數據到inBuf 
  53.          *  
  54.          * @author GV 
  55.          *  
  56.          */  
  57.         class RecordThread extends Thread {  
  58.                 private int recBufSize;  
  59.                 private AudioRecord audioRecord;  
  60.                 public RecordThread(AudioRecord audioRecord, int recBufSize) {  
  61.                         this.audioRecord = audioRecord;  
  62.                         this.recBufSize = recBufSize;  
  63.                 }  
  64.                 public void run() {  
  65.                         try {  
  66.                                 short[] buffer = new short[recBufSize];  
  67.                                 audioRecord.startRecording();// 開始錄制  
  68.                                 while (isRecording) {  
  69.                                         // 從MIC保存數據到緩沖區  
  70.                                         int bufferReadResult = audioRecord.read(buffer, 0,  
  71.                                                         recBufSize);  
  72.                                         short[] tmpBuf = new short[bufferReadResult / rateX];  
  73.                                         for (int i = 0, ii = 0; i < tmpBuf.length; i++, ii = i  
  74.                                                         * rateX) {  
  75.                                                 tmpBuf[i] = buffer[ii];  
  76.                                         }  
  77.                                         synchronized (inBuf) {//  
  78.                                                 inBuf.add(tmpBuf);// 添加數據  
  79.                                         }  
  80.                                 }  
  81.                                 audioRecord.stop();  
  82.                         } catch (Throwable t) {  
  83.                         }  
  84.                 }  
  85.         };  
  86.         /** 
  87.          * 負責繪制inBuf中的數據 
  88.          *  
  89.          * @author GV 
  90.          *  
  91.          */  
  92.         class DrawThread extends Thread {  
  93.                 private int oldX = 0;// 上次繪制的X坐標  
  94.                 private int oldY = 0;// 上次繪制的Y坐標  
  95.                 private SurfaceView sfv;// 畫板  
  96.                 private int X_index = 0;// 當前畫圖所在屏幕X軸的坐標  
  97.                 private Paint mPaint;// 畫筆  
  98.                 public DrawThread(SurfaceView sfv, Paint mPaint) {  
  99.                         this.sfv = sfv;  
  100.                         this.mPaint = mPaint;  
  101.                 }  
  102.                 public void run() {  
  103.                         while (isRecording) {  
  104.                                 ArrayList buf = new ArrayList();  
  105.                                 synchronized (inBuf) {  
  106.                                         if (inBuf.size() == 0)  
  107.                                                 continue;  
  108.                                         buf = (ArrayList) inBuf.clone();// 保存  
  109.                                         inBuf.clear();// 清除  
  110.                                 }  
  111.                                 for (int i = 0; i < buf.size(); i++) {  
  112.                                         short[] tmpBuf = buf.get(i);  
  113.                                         SimpleDraw(X_index, tmpBuf, rateY, baseLine);// 把緩沖區數據畫出來  
  114.                                         X_index = X_index + tmpBuf.length;  
  115.                                         if (X_index > sfv.getWidth()) {  
  116.                                                 X_index = 0;  
  117.                                         }  
  118.                                 }  
  119.                         }  
  120.                 }  
  121.                 /** 
  122.                  * 繪制指定區域 
  123.                  *  
  124.                  * @param start 
  125.                  *            X軸開始的位置(全屏) 
  126.                  * @param buffer 
  127.                  *            緩沖區 
  128.                  * @param rate 
  129.                  *            Y軸數據縮小的比例 
  130.                  * @param baseLine 
  131.                  *            Y軸基線 
  132.                  */  
  133.                 void SimpleDraw(int start, short[] buffer, int rate, int baseLine) {  
  134.                         if (start == 0)  
  135.                                 oldX = 0;  
  136.                         Canvas canvas = sfv.getHolder().lockCanvas(  
  137.                                         new Rect(start, 0, start + buffer.length, sfv.getHeight()));// 關鍵:獲取畫布  
  138.                         canvas.drawColor(Color.BLACK);// 清除背景  
  139.                         int y;  
  140.                         for (int i = 0; i < buffer.length; i++) {// 有多少畫多少  
  141.                                 int x = i + start;  
  142.                                 y = buffer[i] / rate + baseLine;// 調節縮小比例,調節基准線  
  143.                                 canvas.drawLine(oldX, oldY, x, y, mPaint);  
  144.                                 oldX = x;  
  145.                                 oldY = y;  
  146.                         }  
  147.                         sfv.getHolder().unlockCanvasAndPost(canvas);// 解鎖畫布,提交畫好的圖像  
  148.                 }  
  149.         }  
  150. }  

       testOscilloscope.java是主程序,控制UI和ClsOscilloscope,代碼如下:

Java代碼
  1. package com.testOscilloscope;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.media.AudioFormat;  
  6. import android.media.AudioRecord;  
  7. import android.media.MediaRecorder;  
  8. import android.os.Bundle;  
  9. import android.view.MotionEvent;  
  10. import android.view.SurfaceView;  
  11. import android.view.View;  
  12. import android.view.View.OnTouchListener;  
  13. import android.widget.Button;  
  14. import android.widget.ZoomControls;  
  15. public class testOscilloscope extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.         Button btnStart,btnExit;  
  18.         SurfaceView sfv;  
  19.     ZoomControls zctlX,zctlY;  
  20.       
  21.     ClsOscilloscope clsOscilloscope=new ClsOscilloscope();  
  22.       
  23.         static final int frequency = 8000;//分辨率  
  24.         static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
  25.         static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;  
  26.         static final int xMax = 16;//X軸縮小比例最大值,X軸數據量巨大,容易產生刷新延時  
  27.         static final int xMin = 8;//X軸縮小比例最小值  
  28.         static final int yMax = 10;//Y軸縮小比例最大值  
  29.         static final int yMin = 1;//Y軸縮小比例最小值  
  30.           
  31.         int recBufSize;//錄音最小buffer大小  
  32.         AudioRecord audioRecord;  
  33.         Paint mPaint;  
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.         //錄音組件  
  39.                 recBufSize = AudioRecord.getMinBufferSize(frequency,  
  40.                                 channelConfiguration, audioEncoding);  
  41.                 audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,  
  42.                                 channelConfiguration, audioEncoding, recBufSize);  
  43.                 //按鍵  
  44.                 btnStart = (Button) this.findViewById(R.id.btnStart);  
  45.                 btnStart.setOnClickListener(new ClickEvent());  
  46.                 btnExit = (Button) this.findViewById(R.id.btnExit);  
  47.                 btnExit.setOnClickListener(new ClickEvent());  
  48.                 //畫板和畫筆  
  49.                 sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);   
  50.                 sfv.setOnTouchListener(new TouchEvent());  
  51.         mPaint = new Paint();    
  52.         mPaint.setColor(Color.GREEN);// 畫筆為綠色    
  53.         mPaint.setStrokeWidth(1);// 設置畫筆粗細   
  54.         //示波器類庫  
  55.         clsOscilloscope.initOscilloscope(xMax/2, yMax/2, sfv.getHeight()/2);  
  56.           
  57.         //縮放控件,X軸的數據縮小的比率高些  
  58.                 zctlX = (ZoomControls)this.findViewById(R.id.zctlX);  
  59.                 zctlX.setOnZoomInClickListener(new View.OnClickListener() {  
  60.                         @Override  
  61.                         public void onClick(View v) {  
  62.                                 if(clsOscilloscope.rateX>xMin)  
  63.                                         clsOscilloscope.rateX--;  
  64.                                 setTitle("X軸縮小"+String.valueOf(clsOscilloscope.rateX)+"倍"  
  65.                                                 +","+"Y軸縮小"+String.valueOf(clsOscilloscope.rateY)+"倍");  
  66.                         }  
  67.                 });  
  68.                 zctlX.setOnZoomOutClickListener(new View.OnClickListener() {  
  69.                         @Override  
  70.                         public void onClick(View v) {  
  71.                                 if(clsOscilloscope.rateX<xmax)  
  72.                                         clsOscilloscope.rateX++;          
  73.                                 setTitle("X軸縮小"+String.valueOf(clsOscilloscope.rateX)+"倍"  
  74.                                                 +","+"Y軸縮小"+String.valueOf(clsOscilloscope.rateY)+"倍");  
  75.                         }  
  76.                 });  
  77.                 zctlY = (ZoomControls)this.findViewById(R.id.zctlY);  
  78.                 zctlY.setOnZoomInClickListener(new View.OnClickListener() {  
  79.                         @Override  
  80.                         public void onClick(View v) {  
  81.                                 if(clsOscilloscope.rateY>yMin)  
  82.                                         clsOscilloscope.rateY--;  
  83.                                 setTitle("X軸縮小"+String.valueOf(clsOscilloscope.rateX)+"倍"  
  84.                                                 +","+"Y軸縮小"+String.valueOf(clsOscilloscope.rateY)+"倍");  
  85.                         }  
  86.                 });  
  87.                   
  88.                 zctlY.setOnZoomOutClickListener(new View.OnClickListener() {  
  89.                         @Override  
  90.                         public void onClick(View v) {  
  91.                                 if(clsOscilloscope.rateY<ymax)  
  92.                                         clsOscilloscope.rateY++;          
  93.                                 setTitle("X軸縮小"+String.valueOf(clsOscilloscope.rateX)+"倍"  
  94.                                                 +","+"Y軸縮小"+String.valueOf(clsOscilloscope.rateY)+"倍");  
  95.                         }  
  96.                 });  
  97.     }  
  98.         @Override  
  99.         protected void onDestroy() {  
  100.                 super.onDestroy();  
  101.                 android.os.Process.killProcess(android.os.Process.myPid());  
  102.         }  
  103.           
  104.         /** 
  105.          * 按鍵事件處理 
  106.          * @author GV 
  107.          * 
  108.          */  
  109.         class ClickEvent implements View.OnClickListener {  
  110.                 @Override  
  111.                 public void onClick(View v) {  
  112.                         if (v == btnStart) {  
  113.                                 clsOscilloscope.baseLine=sfv.getHeight()/2;  
  114.                                 clsOscilloscope.Start(audioRecord,recBufSize,sfv,mPaint);  
  115.                         } else if (v == btnExit) {  
  116.                                 clsOscilloscope.Stop();  
  117.                         }  
  118.                 }  
  119.         }  
  120.         /** 
  121.          * 觸摸屏動態設置波形圖基線 
  122.          * @author GV 
  123.          * 
  124.          */  
  125.         class TouchEvent implements OnTouchListener{  
  126.                 @Override  
  127.                 public boolean onTouch(View v, MotionEvent event) {  
  128.                         clsOscilloscope.baseLine=(int)event.getY();  
  129.                         return true;  
  130.                 }  
  131.                   
  132.         }  
  133. }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved