问题
I wanted to plot real time data via http://www.android-graphview.org/ for data acquired in a Bluetooth thread.
Thread code:
InputStream tmpIn = mSocket.getInputStream();
while (true) {
try {
BufferedReader r = new BufferedReader(new InputStreamReader(tmpIn));
String line;
while ((line = r.readLine()) != null) {
final String tmp = line;
runOnUiThread(new Runnable() {
@Override
public void run() {
addData(Integer.parseInt(tmp));
}
});
}
} catch (IOException e) {
Log.e("BT",
"BtConnectionThread run while loop: problem reading");
e.printStackTrace();
break;
}
}
}
Activity code:
public void addData(int data){
series.appendData(new DataPoint(lastx,data),true,winSize);
lastx++;
}
This works perfectly, but gets extremely laggy over time. The BT thread receives data with 100Hz - after the first few hundred data sets the memory usage is immense and the graph begins to lag. Is there a workaround or an alternative ringbuffer implementation?
Additional i wanted to disable the x-axis legend, but couln't find any command to archive this.
Regards, Lukas
回答1:
First things first, you can hide the labels of the x-axis (provided that is what you want to do) by casting the following method :
your_graph.getGridLabelRenderer().setHorizontalLabelsVisible( false );
As for the laggyness part, I have experienced it too on graphs with a large set of points. The idea of a circular buffer seems to be a good one, if you do not need to visualize the whole history of your data. I would pair it with the
your_series.resetData( dataPoint[] my_data_points );
method to ensure a live update of the graph. Your addData function would append data to the circular buffer, which you would pass to the method above to update the graph on a timely basis.
I fear this can be quite resource-consuming if you want to refresh the graph at a high rate and with a large number of points, but you at least you would be able to control those two parameters.
回答2:
you could reuse the datapoint objects. The problem is that you create new objects and when the heap is full the jvm has to gc it.
so try to reuse the objects in some way
来源:https://stackoverflow.com/questions/29607087/android-graph-view-laggy-on-huge-data