Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - snapshot bitmap contains view based content (#9252)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jun 14, 2017
1 parent c68872a commit 97cb60e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.CannotAddSourceException;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.utils.BitmapUtils;
import com.mapbox.services.commons.geojson.Feature;

import java.nio.ByteBuffer;
Expand All @@ -39,7 +40,6 @@

import timber.log.Timber;


// Class that wraps the native methods for convenience
final class NativeMapView {

Expand Down Expand Up @@ -920,12 +920,20 @@ protected void onMapChanged(int rawChange) {
}

protected void onFpsChanged(double fps) {
if (isDestroyedOn("OnFpsChanged")) {
return;
}
mapView.onFpsChanged(fps);
}

protected void onSnapshotReady(Bitmap bitmap) {
if (snapshotReadyCallback != null && bitmap != null) {
snapshotReadyCallback.onSnapshotReady(bitmap);
protected void onSnapshotReady(Bitmap mapContent) {
if (isDestroyedOn("OnSnapshotReady")) {
return;
}

Bitmap viewContent = BitmapUtils.createBitmapFromView(mapView);
if (snapshotReadyCallback != null && mapContent != null && viewContent != null) {
snapshotReadyCallback.onSnapshotReady(BitmapUtils.mergeBitmap(mapContent, viewContent));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mapbox.mapboxsdk.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.view.View;

public class BitmapUtils {

public static Bitmap createBitmapFromView(@NonNull View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
view.buildDrawingCache();

if (view.getDrawingCache() == null) {
return null;
}

Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
return snapshot;
}

public static Bitmap mergeBitmap(@NonNull Bitmap background, @NonNull Bitmap foreground) {
Bitmap result = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(background, 0f, 0f, null);
canvas.drawBitmap(foreground, 10, 10, null);
return result;
}

}

0 comments on commit 97cb60e

Please sign in to comment.