android 波形图

From:http://blog.csdn.net/jjdujiang/article/details/51811583

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



附上主要代码

[java]  view plain  copy
  1. // 背景  
  2.         p.setStyle(Style.FILL);  
  3.         p.setColor(Color.WHITE);  
  4.         p.setStrokeWidth(1);  
  5.         canvas.drawRect(00, width, height, p);  
  6.           
  7.         // 图形区  
  8.         p.setStyle(Style.STROKE);  
  9.         p.setColor(Color.BLACK);  
  10.         canvas.drawRect(rectRect, p);  
  11.           
  12.         // Y轴刻度  
  13.         p.setStyle(Style.FILL);  
  14.         for (int i = 0; i < strYScaleStrings.length; i++) {  
  15.             Integer yInteger = rectRect.top + i * rectRect.height() / (strYScaleStrings.length - 1);  
  16.             canvas.drawLine(rectRect.left, yInteger, rectRect.left - nInterInteger, yInteger, p);  
  17.             canvas.drawText(strYScaleStrings[i], nInterInteger, yInteger, p);   
  18.         }  
  19.           
  20.         // X轴刻度  
  21.         for (int i = 0; i < strXScaleStrings.length; i++) {  
  22.             Integer xInteger = rectRect.left + i * rectRect.width() / (strYScaleStrings.length - 1);  
  23.             Integer xTextStartInteger = xInteger - rectXScaleWidthIntegers[i].width() / 2;  
  24.             canvas.drawLine(xInteger, rectRect.bottom, xInteger, rectRect.bottom + nInterInteger, p);  
  25.             canvas.drawText(strXScaleStrings[i], xTextStartInteger, height - rectXScaleWidthIntegers[i].height() / 2, p);   
  26.         }  
  27.           
  28.         // X轴网格线  
  29.         p.setColor(Color.LTGRAY);  
  30.         p.setStrokeWidth(1);         
  31.         p.setPathEffect(effects);  
  32.         for (int i = 0; i < strXScaleStrings.length - 2; i++) {  
  33.             Integer xInteger = rectRect.left + (i + 1) * rectRect.width() / (strYScaleStrings.length - 1);  
  34.             canvas.drawLine(xInteger, rectRect.bottom, xInteger, rectRect.top, p);  
  35.         }    
  36.         // Y轴网格线  
  37.         for (int i = 0; i < strYScaleStrings.length - 2; i++) {  
  38.             Integer yInteger = rectRect.top + (i + 1) * rectRect.height() / (strYScaleStrings.length - 1);  
  39.             canvas.drawLine(rectRect.left, yInteger, rectRect.right, yInteger, p);  
  40.         }  
  41.           
  42.         // 画曲线  
  43.         p.setStyle(Style.STROKE);  
  44.         p.setPathEffect(null);  
  45.         linePath.reset();  
  46.         for (int i = 0; i < nPointDrawCountInteger; i++) {  
  47.             double xValueDouble = (nPointStartInteger + i) / dbSampleFrequencyDouble;  
  48.             double yValueDouble = app.ch1ValueArrayDoubles[i];  
  49.               
  50.             double dXInteger = rectRect.left +   
  51.                     (xValueDouble - dbXMinDouble) * rectRect.width() / (dbXMaxDouble - dbXMinDouble);  
  52.             int nXInteger = (int)(dXInteger + 0.5);  
  53.             double dYIntegerDouble = rectRect.top +   
  54.                     (yValueDouble - dbYMinDouble) * rectRect.height() / (dbYMaxDouble - dbYMinDouble);  
  55.             int nYInteger = (int)(dYIntegerDouble + 0.5);  
  56.             if (0 == i) {  
  57.                 linePath.moveTo(nXInteger, nYInteger);  
  58.             } else {  
  59.                 linePath.lineTo(nXInteger, nYInteger);  
  60.             }  
  61.         }  
  62.         p.setColor(Color.BLUE);  
  63.         p.setStrokeWidth(3);    // 线条粗细  
  64.         canvas.drawPath(linePath, p);  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 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. 运行应用程序,即可看到绘制的波形图。 注意:此示例只是一个简单的示例,实际应用中需要考虑更多的因素,例如波形图的数据来源、缩放、滚动等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值