如何将AChartEngine图表转换为位图

问题描述 投票:0回答:3

我想把我用AChartEngine lib做的一个线图转换成位图,我应该怎么做?

更新:我用这个方法。

 public static Bitmap loadBitmapFromView(View v) {  
v.setDrawingCacheEnabled(true);
v.layout( 0,0,800,600);
v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
v.buildDrawingCache();
v.getDrawingCache();
 Bitmap bmp = Bitmap.createBitmap(800,600, Bitmap.Config.ARGB_8888);
v.setDrawingCacheEnabled(false);
return bmp; }

并将结果保存在一个png文件中,但我得到的是一个空文件!

android bitmap achartengine
3个回答
0
投票

toBitmap()方法似乎总是返回null。

试试这个。

            //Get the graphical view 
            GraphicalView v = ChartFactory.getLineChartView(context, 
                            buildDataset(titles, x, values), renderer); 

           //Enable the cache 
            v.setDrawingCacheEnabled(true); 

            //Set the layout manually to 800*600 
            v.layout(0, 0, 800, 600); 

            //Set the quality to high 
            v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 

            //Build the cache, get the bitmap and close the cache 
            v.buildDrawingCache(true); 
            Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); 
            v.setDrawingCacheEnabled(false); 

0
投票

有点晚了,但这是我从GraphicalView创建JPG(通过Bitmap)的方法。

Bitmap bmp = Bitmap.createBitmap( 600, 1000, Bitmap.Config.ARGB_8888 );
Canvas canvas = new Canvas(bmp);
graphicalView.draw( canvas );

FileOutputStream out = new FileOutputStream( filename );
bmp.compress( Bitmap.CompressFormat.JPEG, 97, out );
out.close();

0
投票

SVG对于图表来说更有效。我黑了AChartEngine来输出SVG,用于我们正在使用的特定类型的图表。这个黑客很容易扩展到其他图表。如果有人对此感兴趣,请回复这个答案,我会把我做的事情写成Gist。

© www.soinside.com 2019 - 2024. All rights reserved.