[android] Add a method to make map snapshots

Fixes: https://github.com/mapbox/mapbox-gl-native/issues/3466
This commit is contained in:
Vladimir Kurchatkin
2016-03-29 18:00:17 +03:00
committed by Tobrun
parent e692096581
commit 862305d935
6 changed files with 211 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.FloatRange;
@@ -2453,6 +2454,28 @@ public class MapView extends FrameLayout {
mMapboxMap = mapboxMap;
}
@UiThread
void snapshot(@NonNull final MapboxMap.SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
final TextureView textureView = (TextureView) findViewById(R.id.textureView);
final boolean canUseBitmap = bitmap != null && textureView.getWidth() == bitmap.getWidth() && textureView.getHeight() == bitmap.getHeight();
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
if (canUseBitmap) {
return textureView.getBitmap(bitmap);
} else {
return textureView.getBitmap();
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
callback.onSnapshotReady(bitmap);
}
}.execute();
}
//
// View utility methods
//

View File

@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.maps;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.SystemClock;
import android.support.annotation.FloatRange;
@@ -1468,6 +1469,16 @@ public class MapboxMap {
mMapView.update();
}
@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
mMapView.snapshot(callback, bitmap);
}
@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback) {
mMapView.snapshot(callback, null);
}
//
// Interfaces
//
@@ -1689,6 +1700,10 @@ public class MapboxMap {
void onFinish();
}
public interface SnapshotReadyCallback {
void onSnapshotReady(Bitmap snapshot);
}
private class MapChangeCameraPositionListener implements MapView.OnMapChangedListener {
private static final long UPDATE_RATE_MS = 400;