用地图的android截图

时间:2014-03-15 06:24:50

标签: android google-maps screenshot

enter image description here我正在尝试生成一个屏幕截图,其中包含一张地图(约占屏幕的2/3),并在视图的上半部分向用户提供信息。结果很糟糕:地图向上移动到顶部并与其他数据混合。这是我使用的代码:

public void screenshot() {
    com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback callback=
            new com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback(){

        @Override
        public void onSnapshotReady(Bitmap snapshot) {

            View v = findViewById(R.id.mainmtc);
            v.setDrawingCacheEnabled(true);
            Bitmap backBitmap = v.getDrawingCache();
            tripimage = Bitmap.createBitmap(
            backBitmap.getWidth(), backBitmap.getHeight(),
            backBitmap.getConfig());
            Canvas canvas = new Canvas(tripimage);
            canvas.drawBitmap(snapshot, new Matrix(), null);
            canvas.drawBitmap(backBitmap, 0, 0, null);
        }
    };

map.snapshot(callback);

}

2 个答案:

答案 0 :(得分:0)

我认为以下链接对您有用:

your reference link

我希望它与你有用的链接。

答案 1 :(得分:0)

您必须在屏幕上获取mapView位置

int[] mapLocation = new int[2];
mapView.getLocationOnScreen(mapLocation);

我已编辑您的代码:

    public void screenshot() {
    com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback callback =
        new com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback() {

          @Override
          public void onSnapshotReady(Bitmap snapshot) {

            View v = findViewById(R.id.mainmtc);
            v.setDrawingCacheEnabled(true);
            Bitmap backBitmap = v.getDrawingCache();
            Canvas canvas = new Canvas(backBitmap);

            Paint paint = new Paint();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
            canvas.drawBitmap(snapshot, mapLocation[0],mapLocation[1], paint);
          }
        };
}

它应该可以正常工作。