4

I have written a code which plots a Line Graph. This graph is plotted by using Android Plot.. How can i save this graph as .png image??

4

4 回答 4

5
        xyPlot.setDrawingCacheEnabled(true);
        int width = xyPlot.getWidth();
        int height = xyPlot.getHeight();
        xyPlot.measure(width, height);
        Bitmap bmp = Bitmap.createBitmap(xyPlot.getDrawingCache());
        xyPlot.setDrawingCacheEnabled(false);
        FileOutputStream fos = new FileOutputStream(fullFileName, true);
        bmp.compress(CompressFormat.JPEG, 100, fos);
于 2011-10-17T11:34:49.150 回答
4

您可以通过以下方式将任何视图的绘图缓存作为位图获取:

Bitmap bitmap = view.getDrawingCache();

然后你可以简单地将位图保存到一个文件中:

FileOutputStream fos = c.openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();

此示例将位图保存到只有您的应用程序才能访问的本地存储中。有关保存文件的更多信息,请查看文档:http: //developer.android.com/guide/topics/data/data-storage.html

于 2011-09-23T17:24:58.013 回答
1

在调用方法之前Bitmap bitmap = view.getDrawingCache();,您必须调用方法view.setDrawingCacheEnabled(true)

无论如何,它不适用于所有视图,如果您的视图扩展 SurfaceView,则返回的位图将是黑色图像。在这种情况下,您必须使用视图的方法 draw (链接到另一篇文章)

PS:slayton,如果我能写评论,我会评论你的帖子,但我没有足够的声誉

于 2011-09-23T17:51:35.573 回答
0

我找到了一个png格式的解决方案:

plot = (XYPlot) findViewById(R.id.pot);
plot.layout(0, 0, 400, 200);

XYSeries series = new SimpleXYSeries(Arrays.asList(array1),Arrays.asList(array2),"series");

            LineAndPointFormatter seriesFormat = new LineAndPointFormatter();
            seriesFormat.setPointLabelFormatter(new PointLabelFormatter());

            plot.addSeries(series, seriesFormat);

            plot.setDrawingCacheEnabled(true);

 FileOutputStream fos = new FileOutputStream("/sdcard/DCIM/img.png", true);

            plot.getDrawingCache().compress(CompressFormat.PNG, 100, fos);

            fos.close();

            plot.setDrawingCacheEnabled(false);
于 2014-12-05T20:10:12.267 回答