I want to save a .png image from AndroidPlot

家住魔仙堡 提交于 2019-12-23 12:29:15

问题


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??


回答1:


        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);



回答2:


You can get the drawing cache of any View as a bitmap with:

Bitmap bitmap = view.getDrawingCache();

Then you can simply save the bitmap to a file with:

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

This example will save the bitmap to the local storage which is only accessible by your app. For more information about saving files check out the docs: http://developer.android.com/guide/topics/data/data-storage.html




回答3:


Before call method Bitmap bitmap = view.getDrawingCache(); you have to call the method view.setDrawingCacheEnabled(true).

Anyway it doesn't work on all Views, if your View extends SurfaceView the bitmap returned will be a black image. In that cases you have to use the method draw of your view (link to another post).

P.S.: slayton if I could write comments I would comment your post but I haven't got enought reputation




回答4:


I found a solution in png format:

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);


来源:https://stackoverflow.com/questions/7532458/i-want-to-save-a-png-image-from-androidplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!