龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android提高之模拟信号示波器的实现(2)

时间:2014-08-10 02:38来源:网络整理 作者:网络 点击:
分享到:
testOscilloscope.java是主程序,控制UI和ClsOscilloscope,代码如下: package com.testOscilloscope;import android.app.Activity;import android.graphics.Color;import android.graphics.Paint;im

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

package com.testOscilloscope;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ZoomControls;
public class testOscilloscope extends Activity {
  /** Called when the activity is first created. */
 Button btnStart,btnExit;
 SurfaceView sfv;
  ZoomControls zctlX,zctlY;
  
  ClsOscilloscope clsOscilloscope=new ClsOscilloscope();
  
 static final int frequency = 8000;//分辨率
 static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
 static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
 static final int xMax = 16;//X轴缩小比例最大值,X轴数据量巨大,容易产生刷新延时
 static final int xMin = 8;//X轴缩小比例最小值
 static final int yMax = 10;//Y轴缩小比例最大值
 static final int yMin = 1;//Y轴缩小比例最小值
 
 int recBufSize;//录音最小buffer大小
 AudioRecord audioRecord;
 Paint mPaint;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //录音组件
 recBufSize = AudioRecord.getMinBufferSize(frequency,
  channelConfiguration, audioEncoding);
 audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
  channelConfiguration, audioEncoding, recBufSize);
 //按键
 btnStart = (Button) this.findViewById(R.id.btnStart);
 btnStart.setOnClickListener(new ClickEvent());
 btnExit = (Button) this.findViewById(R.id.btnExit);
 btnExit.setOnClickListener(new ClickEvent());
 //画板和画笔
 sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01); 
 sfv.setOnTouchListener(new TouchEvent());
    mPaint = new Paint(); 
    mPaint.setColor(Color.GREEN);// 画笔为绿色 
    mPaint.setStrokeWidth(1);// 设置画笔粗细 
    //示波器类库
    clsOscilloscope.initOscilloscope(xMax/2, yMax/2, sfv.getHeight()/2);
    
    //缩放控件,X轴的数据缩小的比率高些
 zctlX = (ZoomControls)this.findViewById(R.id.zctlX);
 zctlX.setOnZoomInClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(clsOscilloscope.rateX>xMin)
   clsOscilloscope.rateX--;
  setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
   +","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
  }
 });
 zctlX.setOnZoomOutClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(clsOscilloscope.rateX<xMax)
   clsOscilloscope.rateX++; 
  setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
   +","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
  }
 });
 zctlY = (ZoomControls)this.findViewById(R.id.zctlY);
 zctlY.setOnZoomInClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(clsOscilloscope.rateY>yMin)
   clsOscilloscope.rateY--;
  setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
   +","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
  }
 });
 
 zctlY.setOnZoomOutClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if(clsOscilloscope.rateY<yMax)
   clsOscilloscope.rateY++; 
  setTitle("X轴缩小"+String.valueOf(clsOscilloscope.rateX)+"倍"
   +","+"Y轴缩小"+String.valueOf(clsOscilloscope.rateY)+"倍");
  }
 });
  }
 @Override
 protected void onDestroy() {
 super.onDestroy();
 android.os.Process.killProcess(android.os.Process.myPid());
 }
 
 /**
 * 按键事件处理
 * @author GV
 *
 */
 class ClickEvent implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  if (v == btnStart) {
  clsOscilloscope.baseLine=sfv.getHeight()/2;
  clsOscilloscope.Start(audioRecord,recBufSize,sfv,mPaint);
  } else if (v == btnExit) {
  clsOscilloscope.Stop();
  }
 }
 }
 /**
 * 触摸屏动态设置波形图基线
 * @author GV
 *
 */
 class TouchEvent implements OnTouchListener{
 @Override
 public boolean onTouch(View v, MotionEvent event) {
  clsOscilloscope.baseLine=(int)event.getY();
  return true;
 }
 }
}

希望本文实例对于读者进行Android项目开发能起到一定的借鉴与帮助作用。

精彩图集

赞助商链接