我正在使用jjoe64's GraphView project,但我在动态添加数据时遇到了问题。我正在尝试在线图上绘制麦克风音量,但图表拒绝更新。这就是我所拥有的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.GraphLayout);
graph = new LineGraphView(this, "Sounds");
data = new GraphViewSeries(new GraphView.GraphViewData[] { new GraphView.GraphViewData(X++, 0) });
graph.addSeries(data);
graph.setManualYAxis(true);
graph.setManualYAxisBounds(32767, 0);
graph.setViewPort(3, 100);
graph.setScrollable(true);
layout.addView(graph);
final MediaRecorder media = new MediaRecorder();
media.setAudioSource(MediaRecorder.AudioSource.MIC);
media.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
media.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
media.setOutputFile("/dev/null");
try {
media.prepare();
} catch (Throwable t) {}
media.start();
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final int amp = media.getMaxAmplitude();
MaxSoFar = Math.max(amp, MaxSoFar);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
data.appendData(new GraphView.GraphViewData(X++, amp), true);
//Here I try few different things
TextView output = (TextView)findViewById(R.id.output);
String info = MaxSoFar + "" + nl;
info += amp + "";
output.setText(info);
}
});
}
}, 0, 100);
}
我在data.appendData(...)
graph.redrawAll();
,graph.invalidate();
或graph.postInvalidate();
之后立即进行了很多实验,但没有。如果我放下手指并拖动图形,我只能重绘图形。任何类型的触摸输入都将使图形正确绘制。它可能与它在runOnUiThread
中有关,但我想不出为什么这可能是一个问题,而不是一个要求。
我在运行Cyanogenmod 10.1 Nightlies(Android 4.2.2)的Galaxy S3上进行调试。