我想把我用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文件中,但我得到的是一个空文件!
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);
有点晚了,但这是我从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();
SVG对于图表来说更有效。我黑了AChartEngine来输出SVG,用于我们正在使用的特定类型的图表。这个黑客很容易扩展到其他图表。如果有人对此感兴趣,请回复这个答案,我会把我做的事情写成Gist。