开发者

I want to save a .png image from AndroidPlot

开发者 https://www.devze.com 2023-04-06 20:28 出处:网络
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 .开发者_如何学Cpng image??xyPlot.setDrawingCacheEnabled(true);

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 .开发者_如何学Cpng image??


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消