问题描述
- achartengine内存泄露
-
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_oxy_chat); back = (RelativeLayout) findViewById(R.id.activity_oxy_chat_back); chatLayout = (LinearLayout) findViewById(R.id.oxy_chat); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(OxyLineChatActivity.this, DeviceOptActivity.class)); finish(); } }); initView(); // chatLayout.setLayoutParams(new // LayoutParams(LayoutParams.MATCH_PARENT, // // LayoutParams.MATCH_PARENT)); // chatLayout.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, // LayoutParams.MATCH_PARENT)); } @Override protected void onResume() { super.onResume(); if (view == null) { view = ChartFactory.getLineChartView(this,_ buildDataset(titles, x, values), renderer); view.setBackgroundColor(Color.WHITE); // enable the chart click events chatLayout.addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } else { ((GraphicalView) view).repaint(); } } public void initView() { titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" }; x = new ArrayList<double[]>(); for (int i = 0; i < titles.length; i++) { x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); } values = new ArrayList<double[]>(); values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 }); values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 }); values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 }); values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 }); // 鐐圭殑鏍峰紡 int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW }; PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE }; renderer = buildRenderer(colors, styles); int length = renderer.getSeriesRendererCount(); // 鐐规槸绌哄績杩樻槸瀹炲績 for (int i = 0; i < length; i++) { ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)) .setFillPoints(true); } setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY); // 杞翠笂鏁板瓧鐨勬暟閲? renderer.setXLabels(12); renderer.setYLabels(10); // 鏄?惁鏄剧ず缃戞牸 renderer.setShowGrid(true); // x鎴杫杞翠笂鏁板瓧鐨勬柟鍚戯紝鐩稿弽鐨勩?? renderer.setXLabelsAlign(Align.RIGHT); renderer.setYLabelsAlign(Align.RIGHT); // 缂╂斁鏁堟灉 renderer.setZoomButtonsVisible(true); renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); renderer.setZoomLimits(new double[] { -10, 20, -10, 40 }); // 鍥捐〃閮ㄥ垎鐨勮儗鏅??鑹? // renderer.setApplyBackgroundColor(true);// 璁剧疆鏄?惁鏄剧ず鑳屾櫙鑹? // renderer.setBackgroundColor(Color.WHITE); // renderer.setBackgroundColor(Color.parseColor("#ffffff")); // renderer.setApplyBackgroundColor(true); // 鍥捐〃涓庡睆骞曞洓杈圭殑闂磋窛棰滆壊 renderer.setMarginsColor(Color.WHITE); } private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); setRenderer(renderer, colors, styles); return renderer; } private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) { renderer.setAxisTitleTextSize(16); renderer.setChartTitleTextSize(20); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); renderer.setPointSize(5f); renderer.setMargins(new int[] { 20, 30, 15, 20 }); int length = colors.length; for (int i = 0; i < length; i++) { XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(colors[i]); r.setPointStyle(styles[i]); renderer.addSeriesRenderer(r); } } private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) { renderer.setChartTitle(title); renderer.setXTitle(xTitle); renderer.setYTitle(yTitle); renderer.setXAxisMin(xMin); renderer.setXAxisMax(xMax); renderer.setYAxisMin(yMin); renderer.setYAxisMax(yMax); renderer.setAxesColor(axesColor); renderer.setLabelsColor(labelsColor); } private XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); addXYSeries(dataset, titles, xValues, yValues, 0); return dataset; } private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) { int length = titles.length; for (int i = 0; i < length; i++) { XYSeries series = new XYSeries(titles[i], scale); double[] xV = xValues.get(i); double[] yV = yValues.get(i); int seriesLength = xV.length; for (int k = 0; k < seriesLength; k++) { series.add(xV[k], yV[k]); } dataset.addSeries(series); } } @Override protected void onDestroy() { super.onDestroy(); if (view != null) { chatLayout.removeView(view); view = null; } }
}
解决方案
解决方案二:
http://www.tuicool.com/articles/n6vEVr
时间: 2024-12-09 12:24:17