1

我正在使用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 操作。

4

2 回答 2

0

它是我的运行代码,请检查它。

公共类 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;
}

}

于 2013-04-20T08:13:12.023 回答
0

不用担心 - GC 只是 Android 垃圾收集系统。它总是在每台设备的后台运行。更有可能的是,GC 只是输出可用的内存和收集的内存。

于 2013-04-19T17:49:26.780 回答