android 波形图

本周开发了android画波形图,单击按钮,进入到新的activity,在新的activity中设置视图类setContentView(lineView),lineView是继承View,重载函数onDraw就可以在画布上画波形图了。



附上主要代码

// 背景
        p.setStyle(Style.FILL);
		p.setColor(Color.WHITE);
		p.setStrokeWidth(1);
        canvas.drawRect(0, 0, width, height, p);
		
        // 图形区
        p.setStyle(Style.STROKE);
        p.setColor(Color.BLACK);
        canvas.drawRect(rectRect, p);
        
        // Y轴刻度
        p.setStyle(Style.FILL);
        for (int i = 0; i < strYScaleStrings.length; i++) {
        	Integer yInteger = rectRect.top + i * rectRect.height() / (strYScaleStrings.length - 1);
        	canvas.drawLine(rectRect.left, yInteger, rectRect.left - nInterInteger, yInteger, p);
        	canvas.drawText(strYScaleStrings[i], nInterInteger, yInteger, p); 
		}
        
        // X轴刻度
        for (int i = 0; i < strXScaleStrings.length; i++) {
        	Integer xInteger = rectRect.left + i * rectRect.width() / (strYScaleStrings.length - 1);
        	Integer xTextStartInteger = xInteger - rectXScaleWidthIntegers[i].width() / 2;
        	canvas.drawLine(xInteger, rectRect.bottom, xInteger, rectRect.bottom + nInterInteger, p);
        	canvas.drawText(strXScaleStrings[i], xTextStartInteger, height - rectXScaleWidthIntegers[i].height() / 2, p); 
		}
        
        // X轴网格线
        p.setColor(Color.LTGRAY);
        p.setStrokeWidth(1);       
        p.setPathEffect(effects);
        for (int i = 0; i < strXScaleStrings.length - 2; i++) {
        	Integer xInteger = rectRect.left + (i + 1) * rectRect.width() / (strYScaleStrings.length - 1);
        	canvas.drawLine(xInteger, rectRect.bottom, xInteger, rectRect.top, p);
		}  
        // Y轴网格线
        for (int i = 0; i < strYScaleStrings.length - 2; i++) {
        	Integer yInteger = rectRect.top + (i + 1) * rectRect.height() / (strYScaleStrings.length - 1);
        	canvas.drawLine(rectRect.left, yInteger, rectRect.right, yInteger, p);
		}
        
        // 画曲线
        p.setStyle(Style.STROKE);
        p.setPathEffect(null);
        linePath.reset();
        for (int i = 0; i < nPointDrawCountInteger; i++) {
        	double xValueDouble = (nPointStartInteger + i) / dbSampleFrequencyDouble;
        	double yValueDouble = app.ch1ValueArrayDoubles[i];
			
        	double dXInteger = rectRect.left + 
					(xValueDouble - dbXMinDouble) * rectRect.width() / (dbXMaxDouble - dbXMinDouble);
			int nXInteger = (int)(dXInteger + 0.5);
			double dYIntegerDouble = rectRect.top + 
					(yValueDouble - dbYMinDouble) * rectRect.height() / (dbYMaxDouble - dbYMinDouble);
			int nYInteger = (int)(dYIntegerDouble + 0.5);
			if (0 == i) {
				linePath.moveTo(nXInteger, nYInteger);
			} else {
				linePath.lineTo(nXInteger, nYInteger);
			}
		}
        p.setColor(Color.BLUE);
        p.setStrokeWidth(3);	// 线条粗细
        canvas.drawPath(linePath, p);


要在 Android 上绘制波形图,可以使用 Android 的 Canvas API 和 Paint API。下面是一个简单的示例,绘制一个正弦波形图: 1. 在 XML 布局文件中添加一个 CustomView: ```xml <com.example.customview.WaveformView android:id="@+id/waveform_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 2. 创建 WaveformView 类,并重写 onDraw() 方法: ```java public class WaveformView extends View { private Paint paint; private Path path; public WaveformView(Context context) { this(context, null); } public WaveformView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(5); paint.setStyle(Paint.Style.STROKE); path = new Path(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); int halfHeight = height / 2; int xStep = 5; int yStep = 50; path.reset(); for (int x = 0; x < width; x += xStep) { float y = halfHeight + (float) Math.sin(x * 2 * Math.PI / yStep) * halfHeight; if (x == 0) { path.moveTo(x, y); } else { path.lineTo(x, y); } } canvas.drawPath(path, paint); } } ``` 此示例使用 Path 对象来存储波形图的路径,并使用 Canvas 的 drawPath() 方法绘制路径。在 onDraw() 方法中,使用 Math.sin() 方法计算正弦函数的值,并将值存储在 Path 中,最后使用 drawPath() 方法绘制波形图。 3. 运行应用程序,即可看到绘制的波形图。 注意:此示例只是一个简单的示例,实际应用中需要考虑更多的因素,例如波形图的数据来源、缩放、滚动等功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值