android - 将 MapView 另存为位图

标签 android google-maps bitmap

我已经尝试了两种方法来将 MapView 另存为位图,我在此处找到了这种方法,但似乎没有一种方法适合我。第一个选项,

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 的位图对象,因此当我尝试将位图用作纹理时,它不会显示。在 mapview 呈现后,我在单击按钮时调用位图保存代码,但它仍然给出相同的结果。

有没有人设法做到这一点?

最佳答案

我在尝试使用绘图缓存时遇到了类似的问题。我发现的一些对我有帮助的项目:

  • 确保立即复制绘图缓存。 Android 也可以回收缓存绘制的位图。我在用它绘制 View 时让 Android 破坏了位图缓存。
  • 出于某种原因,在检索缓存之前销毁它给了我更一致的结果。

这是我正在使用的工作代码:

// 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,但我希望 MapView 的代码路径相同。

最后,在您发布的代码示例中,您将创建 map View 大小的位图。您复制此位图,然后丢弃结果。然后你设置你的 Canvas 由这个位图支持,然后你尝试将位图绘制到位图。 (是的,我输入的是正确的。)试试这样的东西,代替你的 Canvas 代码

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

关于android - 将 MapView 另存为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6221098/

相关文章:

Android:更改 TimePickerDialog 选择器(圆圈)背景颜色

android - 如何在Android中的字符串中插入新行

android - 防止软键盘在 EditText 焦点上打开

javascript - 如何从使用 Google Maps API 的站点恢复地理位置列表

android - 将个人位置添加到 Google Places 自动完成列表

android,面向对象编程与性能设计

google-maps - Google Places API - 下载用户评论和星级

c# - 在屏幕上绘制位图图像

c++ - Allegro draw_sprite()

android - Google Maps V2 android 获取标记图标