canvas教程

Android 模拟信号示波器示例代码(2)

字号+ 作者:H5之家 来源:H5之家 2016-10-06 17:00 我要评论( )

package com.testOscilloscope; import android.app.Activity; import android.graphics.Color; import android.graphics.Paint; import android.media.AudioFormat; import android.media.AudioRecord; import and

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 实现模拟系信号示波器的示例详解,后续继续补充相关知识,谢谢大家对本站 的支持!

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android UI开发专题(五) Bitmap和Canvas实例 Android开发技术

    Android UI开发专题(五) Bitmap和Canvas实例 Android开发技术

    2016-10-05 15:00

  • 我写的Android图表库XCL-Charts,整理好现在开源了!!!

    我写的Android图表库XCL-Charts,整理好现在开源了!!!

    2016-10-02 14:00

  • 在android中画圆形图片的几种办法

    在android中画圆形图片的几种办法

    2016-09-29 11:00

  • Android Canvas中rotate()和translate()方法详解

    Android Canvas中rotate()和translate()方法详解

    2016-09-26 15:00

网友点评