[android] - move snapshot t NativeMapView (#7463)

This commit is contained in:
Tobrun
2017-01-05 00:14:35 +01:00
committed by GitHub
parent b515762528
commit cf859d6d0b
3 changed files with 50 additions and 71 deletions

View File

@@ -8,8 +8,6 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.SurfaceTexture;
@@ -86,7 +84,6 @@ public class MapView extends FrameLayout {
private MapZoomButtonController mapZoomButtonController;
private ConnectivityReceiver connectivityReceiver;
private SnapshotRequest snapshotRequest;
@UiThread
public MapView(@NonNull Context context) {
@@ -609,14 +606,6 @@ public class MapView extends FrameLayout {
}
}
// Called when the map view transformation has changed
// Called via JNI from NativeMapView
// Forward to any listeners
protected void onMapChanged(int mapChange) {
nativeMapView.onMapChangedEventDispatch(mapChange);
}
/**
* Sets a callback object which will be triggered when the {@link MapboxMap} instance is ready to be used.
*
@@ -641,53 +630,6 @@ public class MapView extends FrameLayout {
this.mapboxMap = mapboxMap;
}
//
// Snapshot API
//
@UiThread
void snapshot(@NonNull final MapboxMap.SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
snapshotRequest = new SnapshotRequest(bitmap, callback);
nativeMapView.scheduleTakeSnapshot();
nativeMapView.render();
}
// Called when the snapshot method was executed
// Called via JNI from NativeMapView
// Forward to any listeners
protected void onSnapshotReady(byte[] bytes) {
if (snapshotRequest != null && bytes != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inBitmap = snapshotRequest.getBitmap(); // the old Bitmap to be reused
options.inMutable = true;
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
MapboxMap.SnapshotReadyCallback callback = snapshotRequest.getCallback();
if (callback != null) {
callback.onSnapshotReady(bitmap);
}
}
}
private class SnapshotRequest {
private Bitmap bitmap;
private MapboxMap.SnapshotReadyCallback callback;
SnapshotRequest(Bitmap bitmap, MapboxMap.SnapshotReadyCallback callback) {
this.bitmap = bitmap;
this.callback = callback;
}
public Bitmap getBitmap() {
return bitmap;
}
public MapboxMap.SnapshotReadyCallback getCallback() {
return callback;
}
}
private static class AttributionOnClickListener implements View.OnClickListener, DialogInterface.OnClickListener {
private static final int ATTRIBUTION_INDEX_IMPROVE_THIS_MAP = 2;
@@ -1062,7 +1004,7 @@ public class MapView extends FrameLayout {
}
}
public boolean isInitialLoad() {
boolean isInitialLoad() {
return initialLoad;
}

View File

@@ -1560,8 +1560,7 @@ public final class MapboxMap {
*/
@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
// FIXME 12/02/2016
//mapView.snapshot(callback, bitmap);
nativeMapView.addSnapshotCallback(callback, bitmap);
}
/**
@@ -1571,8 +1570,7 @@ public final class MapboxMap {
*/
@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback) {
// FIXME 12/02/2016
//mapView.snapshot(callback, null);
snapshot(callback, null);
}
/**
@@ -1989,4 +1987,4 @@ public final class MapboxMap {
Transform getTransform() {
return transform;
}
}
}

View File

@@ -3,6 +3,7 @@ package com.mapbox.mapboxsdk.maps;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Build;
@@ -50,6 +51,9 @@ final class NativeMapView {
// Listeners for Map change events
private CopyOnWriteArrayList<MapView.OnMapChangedListener> onMapChangedListeners;
// Listener invoked to return a bitmap of the map
private SnapshotRequest snapshotRequest;
//
// Static methods
//
@@ -603,7 +607,11 @@ final class NativeMapView {
}
protected void onMapChanged(int rawChange) {
mapView.onMapChanged(rawChange);
if (onMapChangedListeners != null) {
for (MapView.OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(rawChange);
}
}
}
protected void onFpsChanged(double fps) {
@@ -611,7 +619,18 @@ final class NativeMapView {
}
protected void onSnapshotReady(byte[] bytes) {
mapView.onSnapshotReady(bytes);
if (snapshotRequest != null && bytes != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inBitmap = snapshotRequest.getBitmap(); // the old Bitmap to be reused
options.inMutable = true;
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
MapboxMap.SnapshotReadyCallback callback = snapshotRequest.getCallback();
if (callback != null) {
callback.onSnapshotReady(bitmap);
}
}
}
//
@@ -833,11 +852,31 @@ final class NativeMapView {
onMapChangedListeners.remove(listener);
}
void onMapChangedEventDispatch(int mapChange) {
if (onMapChangedListeners != null) {
for (MapView.OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(mapChange);
}
//
// Snapshot
//
void addSnapshotCallback(@NonNull MapboxMap.SnapshotReadyCallback callback, @Nullable Bitmap bitmap) {
snapshotRequest = new SnapshotRequest(bitmap, callback);
scheduleTakeSnapshot();
render();
}
private static class SnapshotRequest {
private Bitmap bitmap;
private MapboxMap.SnapshotReadyCallback callback;
SnapshotRequest(Bitmap bitmap, MapboxMap.SnapshotReadyCallback callback) {
this.bitmap = bitmap;
this.callback = callback;
}
public Bitmap getBitmap() {
return bitmap;
}
public MapboxMap.SnapshotReadyCallback getCallback() {
return callback;
}
}
}