我正在使用AChartEngine绘制动态图。但是我发现手机无法显示图表视图并且logcat一直在输出GC信息。我的 TestData.SENSOR_HISTORY_DATA 只有 8 对 value-time。任何人都知道如何解决这个问题?ACE 版本是 1.0。
我从 ChartFactory 获得了一个图形视图,然后将其添加到我自己的线性布局中。
这是我的代码:</p>
public class HistoryDataChartDisp extends Activity {
private XYSeries series;
private XYMultipleSeriesDataset mDataset;
private GraphicalView chart;
private XYMultipleSeriesRenderer renderer;
private HistoryDataParser histDataParser = new HistoryDataParser();
private LinearLayout layout;
private List<String> histValueList;
private List<String> histTimeList;
private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_data_chart_layout);
ini();
updateChart();
}
private void ini()
{
layout = (LinearLayout)findViewById(R.id.history_data_chart_linearLayout);
series = new XYSeries("Title");
mDataset = new XYMultipleSeriesDataset();
renderer = buildRenderer(Color.GREEN, PointStyle.POINT, true);
setBasicChartSettings(renderer);
mDataset.addSeries(series);
//get the chart
chart = ChartFactory.getCubeLineChartView(getApplicationContext(), mDataset, renderer, 0.5f);
layout.addView(chart, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));//将图表添加到布局中去
}
protected void setBasicChartSettings(XYMultipleSeriesRenderer basicRenderer)
{
basicRenderer.setAxesColor(getResources().getColor(R.color.mirror));
basicRenderer.setLabelsColor(getResources().getColor(R.color.mirror));
basicRenderer.setLabelsTextSize(15);
basicRenderer.setXLabels(5);
basicRenderer.setYLabels(10);
basicRenderer.setYLabelsAlign(Align.LEFT);
basicRenderer.setShowGrid(true);
basicRenderer.setGridColor(getResources().getColor(R.color.lightgray));
basicRenderer.setMargins(new int[]{0,0,10,0});
basicRenderer.setMarginsColor(getResources().getColor(R.color.ivory));
basicRenderer.setPointSize(1);
basicRenderer.setShowLegend(false);
basicRenderer.setPanEnabled(false);
basicRenderer.setZoomEnabled(false);
}
protected XYMultipleSeriesRenderer buildRenderer(int color, PointStyle style, boolean fill)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(color);
r.setPointStyle(style);
r.setFillPoints(fill);
r.setLineWidth(2);
renderer.addSeriesRenderer(r);
return renderer;
}
public void updateChart() {
//For test
//TestData.SENSOR_HISTORY_DATA is a String like this:value1,time1;value2,time2;...
//
updateData(TestData.SENSOR_HISTORY_DATA);
chart.invalidate();
}
private void updateData(String response)
{
histDataParser.parse(response);
histValueList = histDataParser.getHistValueList();//get all values String
histTimeList = histDataParser.getHistTimeList();//get all time String in same order with histValueList
mDataset.removeSeries(series);
series.clear();
double x=0,y=0;
int listSize = histTimeList.size();
if(listSize<1)
{
series.add(0, 0);
}
else
{
for (int i = listSize - 1; i >= 0 ; i--)
{
try
{
x = formatter.parse(histTimeList.get(i)).getTime();
y = Float.parseFloat(histValueList.get(i));
} catch (ParseException e) {
e.printStackTrace();
x = 0;y = 0;
}finally{
series.add(x, y);
}
}
}
mDataset.addSeries(series);
}
}
这是 logcat 信息。
.
.
.
05-19 14:07:35.269: D/dalvikvm(26285): GC_FOR_MALLOC freed 21755 objects / 524280 bytes in 35ms
05-19 14:07:35.359: D/dalvikvm(26285): GC_FOR_MALLOC freed 21755 objects / 524280 bytes in 33ms
05-19 14:07:35.449: D/dalvikvm(26285): GC_FOR_MALLOC freed 21755 objects / 524280 bytes in 33ms
.
.
.
(keep outputing)
另外,我得到了这个: Listview 中有很多垃圾收集。该问题的原因是 android:cacheColorHint="#00000000"。所以我想如果我可以做些什么来减少 GC 操作。