我正在使用AChartEngine绘制动态图。但我发现手机无法显示图表视图,而logcat只是保持输出GC信息。我的TestData.SENSOR_HISTORY_DATA只有8对值时间。谁知道如何解决这个问题? ACE版本为1.0。
我从ChartFactory获得了一个graphicView,然后将其添加到我自己的linearlayout中。
这是我的代码:
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)
另外,我得到了这个:Lots of garbage collection in a listview。该问题的原因是android:cacheColorHint =“#00000000”。所以我认为如果我能做些什么来减少GC行动。
答案 0 :(得分:0)
不要担心 - GC只是Android Garbage Collecting系统。它总是会在每个设备的后台运行。 GC很可能只是输出 可用的内存和收集的内容。
答案 1 :(得分:0)
我的runnig代码请检查。
公共类MainActivity扩展了Activity {
public static final String TYPE = "type";
private final XYMultipleSeriesDataset mDataset = getDemoDataset();
private final XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRendererStyling();
if (mChartView == null) {
final LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(this, mDataset,
mRenderer);
// mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else
mChartView.repaint();
}
private void setRendererStyling() {
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.TRANSPARENT);
mRenderer.setMargins(new int[] { 0, 0, 0, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(10);
mRenderer.setMarginsColor(Color.WHITE);
mRenderer.setGridColor(Color.RED);
}
private XYMultipleSeriesDataset getDemoDataset() {
// double[] seriesFirstY = { 20, -20, 67, 180, -45, 24, 99, -34, -8 };
final double[] seriesSecondY = { 10, 80, -40, -20, 135, 24, 199, -34, 80, 23,
45, 34, 232, 234, 234, 2, 4, 2, 2, 4, 45, 45, 4, 534, 3, 4, 54,
44, 54, 343, 34, 234, 3, 4, 4, 3, 645, 654, 34, 45, 34, 3, 34,
34, 34, 34, 34, 34, 34, 3, 4, 34, 4, 34, 34, 34, 34, 43, 34,
34, 10, 80, -40, -20, 135, 24, 199, -34, 80, 23, 45, 34, 232,
234, 234, 2, 4, 2, 2, 4, 45, 45, 4, 534, 3, 4, 54, 44, 54, 343,
34, 234, 3, 4, 4, 3, 645, 654, 34, 45, 34, 3, 34, 34, 34, 34,
34, 34, 34, 3, 4, 34, 4, 34, 34, 34, 34, 43, 34, 34, 34, 43,
34, 34, 10, 80, -40, -20, 135, 24, 199, -34, 80, 23, 45, 34,
232, 234, 234, 2, 4, 2, 2, 4, 45, 45, 4, 534, 3, 4, 54, 44, 54,
343, 34, 234, 3, 4, 4, 3, 645, 654, 34, 45, 34, 3, 34, 34, 34,
34, 34, 34, 34, 3, 4, 34, 4, 34, 34, 34, 34, 43, 34, 34, 34,
43, 34, 34, 10, 80, -40, -20, 135, 24, 199, -34, 80, 23, 45,
34, 43, 34, 34, 34, 43, 34, 34, 10, 80, -40, -20, 135, 24, 199,
-34, 80, 23, 45, 34, 232, 234, 234, 2, 4, 2, 2, 4, 45, 45, 4,
534, 3, 4, 54, 44, 54, 343, 34, 234, 3, 4, 4, 3, 645, 654, 34,
45, 34, 3, 34, 34, 34, 34, 34, 34, 34, 3, 4, 34, 4, 34, 34, 34,
34, 43, 34, 34, 34, 43, 34, 34, 10, 80, -40, -20, 135, 24, 199,
-34, 80, 23, 45, 34, 232, 234, 234, 2, 4, 2, 2, 4, 45, 45, 4,
534, 3, 4, 54, 44, 54, 343, 34, 234, 3, 4, 4, 3, 645, 654, 34,
45, 34, 3, 34, 34, 34, 34, 34, 34, 34, 3, 4, 34, 4, 34, 34, 34,
34, 43, 34, 34 };
final XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
final XYSeries secondSeries = new XYSeries("Graph");
for (int j = 0; j < seriesSecondY.length; j++)
secondSeries.add(j, seriesSecondY[j]);
dataset.addSeries(secondSeries);
return dataset;
}
private XYMultipleSeriesRenderer getDemoRenderer() {
final XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
final XYSeriesRenderer r = new XYSeriesRenderer();
r.setPointStyle(PointStyle.POINT);
r.setColor(Color.GREEN);
renderer.addSeriesRenderer(r);
renderer.setAxesColor(Color.TRANSPARENT);
renderer.setLabelsColor(Color.TRANSPARENT);
return renderer;
}
}