对于此特定应用程序,您可能希望使用Path和SurfaceView“手动”绘制图形.
在初始化期间准备好Paint实例:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.GREEN); //Change to what you want
当您需要更新图形时,清除场景并构建线路径(根据您的需要进行调整):
canvas.drawColor(Color.WHITE);
Path path = new Path();
path.moveTo(0, yourValueAt(0));
for(int sec = 1; sec < 30; sec++)
path.lineTo(sec, yourValueAt(sec));
canvas.drawPath(path, paint);
您也可以使用quadTo或cubicTo而不是lineTo.
如果您希望图形具有实时动画效果(即在数据出现在右侧时向左滑动),您可以使用与着名的LunarLander示例类似的方式在SurfaceView上绘制(以下代码是简化版本):
class DrawingThread extends Thread {
@Override
public void run() {
while (running) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
doDraw(c);
}
} finally {
if (c != null) mSurfaceHolder.unlockCanvasAndPost(c);
}
synchronized (this) {
//Optional but saves battery life.
//You may compute the value to match a given max framerate..
this.wait(SOME_DELAY_IN_MS);
}
}
}
}
通过调用yourSurfaceView.getHolder()获取mSurfaceHolder并且doDraw是你调用canvas.drawPath()和所有绘图代码.