欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

BaiduMap SDK-地图截屏功能实现

程序员文章站 2022-06-10 16:34:08
...

BaiduMap提供了snapshot(SnapshotReadyCallback callback)来实现截屏功能,SnapshotReadyCallback回调函数中会获得截屏的Bitmap,可以对Bitmap进行操作;

        baiduMap = mapView.getMap();
        baiduMap.snapshot(new BaiduMap.SnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap snapBitmap) {
                try {
                    String pathStr = "/mnt/sdcard/baidumap/test" + System.currentTimeMillis() + ".png";
                    File file = new File(pathStr);
                    FileOutputStream outputStream = new FileOutputStream(file);
                    if (snapBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)) {
                        outputStream.flush();
                        outputStream.close();
                    }
                    Toast.makeText(getApplicationContext(), "地图截屏成功,保持图片为:" + pathStr, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "截图保存失败!" , Toast.LENGTH_SHORT).show();
                }
            }
        });