保存图形页面作为位图 [英] Save MapView as a Bitmap

查看:97
本文介绍了保存图形页面作为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过这两种方法,以一个图形页面保存为我已经找到这里,没有似乎为我工作的位图。第一种选择,

I've tried both methods to save a MapView as a bitmap which I've found here and none seem to work for me. The first option,

Bitmap bitMap = mMapView.getDrawingCache();             
mMapView.setDrawingCacheEnabled(true);
bitMap = mMapView.getDrawingCache(true);

和第二个,

Canvas offscreencanvas = new Canvas();
Bitmap bmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(),
                    Bitmap.Config.ARGB_8888);
bmap.copy(Config.ARGB_4444, true);
offscreencanvas.setBitmap(bmap);
offscreencanvas.drawBitmap(bmap, 0, 0, null);

这两种结果在宽度和高度-1位图对象,所以当我再尝试使用位图作为纹理,它不显示。我所说的位图节省code在点击一个按钮,之后的MapView已经呈现,但它仍然给出了同样的结果。

both result in a bitmap object with width and height of -1 so when I then try and use the bitmap as a texture, it doesn't show. I call the bitmap saving code in a button click, after the mapview has rendered but it still gives the same result.

有没有人成功地做到这一点?

Has anyone managed to do this?

推荐答案

我遇到试图利用绘图缓存类似的问题。我发现了一些项目,帮助我:

I encountered similar problems attempting to make use of the drawing cache. Some items I found that helped me:

  • 确保你马上复制绘画缓存。机器人可以和将回收的,高速缓存绘制太多的位图。我有Android的破坏位图缓存,而我画与它的景色。
  • 出于某种原因,破坏高速缓存中检索它给了我更一致的结果了。

下面是工作code我使用的:

Here is the working code I am using:

// Disable caching, destroy the cache, and force a rebuild
imageView.setWillNotCacheDrawing(false);
imageView.destroyDrawingCache();
imageView.buildDrawingCache();

// Copy the drawing cache before the system recycles it
Bitmap cachedImage = Bitmap.createBitmap(imageView.getDrawingCache());

另外请注意,我使用的是的ImageView ,但我希望在code路径是相同的图形页面

Also note that I am using an ImageView, but I would expect the code path to be the same for MapView.

最后,你贴,你正在创建一个位图的地图视图的大小code样本。您复制该位图,然后扔结果了。然后你设置你的画布由该位图进行备份,然后尝试绘制位图位图。 (是的,我输入正确的。)尝试这样的事情,代替你的画布code

Finally in the code sample that you posted, you are creating a bitmap the size of the map view. You copy this bitmap and then throw the result away. Then you set your canvas to be backed by this bitmap, and then you try to draw the bitmap to the bitmap. (Yes I typed that correctly.) Try something like this, in place of your canvas code

Bitmap mapCache = /* Get this using the previous code */
Bitmap bmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(),
                        Bitmap.Config.ARGB_8888);
Canvas offscreencanvas = new Canvas(bmap);
offscreencanvas.drawBitmap(mapCache, 0, 0, null);

这篇关于保存图形页面作为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆