1

enter image description hereI am trying to generate a screenshot that contains a map (about 2/3 of the screen) and information to the user in the upper part of the view. The result is awful: the map is shifted up to the top and mixes up with the other data. Here's the code I use:

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

}

michaelsmith
  • 1,011
  • 1
  • 16
  • 35

1 Answers1

0

You have to get the mapView location on screen

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

and I have edit your code for you to be:

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

It should work fine.

Tarek360
  • 1,251
  • 1
  • 13
  • 19