I'm working with jjoe64's GraphView project but I am having issues when adding data on the fly. I'm trying to plot microphone volumes on a line graph but the graph refuses to update. Here's what I have:
@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);
}
I've been doing a lot of experimenting on the line immediately after data.appendData(...)
with things like graph.redrawAll();
, graph.invalidate();
, or graph.postInvalidate();
but nothing. I can only get the graph to redraw if I put my finger down and drag the graph. Any sort of touch input will cause the graph to draw correctly. It might have something to do with it being in runOnUiThread
but I can't think why that might be an issue and not a requirement.
I'm debugging this on my Galaxy S3 running Cyanogenmod 10.1 Nightlies (Android 4.2.2).