[android] Add a method to make map snapshots
Fixes: https://github.com/mapbox/mapbox-gl-native/issues/3466
This commit is contained in:
committed by
Tobrun
parent
e692096581
commit
862305d935
@@ -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
|
||||
//
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -187,7 +187,14 @@
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_other" />
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".activity.SnapshotActivity"
|
||||
android:description="@string/description_snapshot"
|
||||
android:label="@string/activity_snapshot">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_other" />
|
||||
</activity>
|
||||
|
||||
<!-- Configuration Settings -->
|
||||
<meta-data
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.utils.ApiAccess;
|
||||
|
||||
public class SnapshotActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "SnapshotActivity";
|
||||
|
||||
private MapView mMapView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_snapshot);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
actionBar.setDisplayShowHomeEnabled(true);
|
||||
}
|
||||
|
||||
mMapView = (MapView) findViewById(R.id.cameraMapView);
|
||||
mMapView.setAccessToken(ApiAccess.getToken(this));
|
||||
mMapView.onCreate(savedInstanceState);
|
||||
mMapView.getMapAsync(new OnMapReadyCallback() {
|
||||
@Override
|
||||
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
|
||||
// set a style
|
||||
mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() {
|
||||
@Override
|
||||
public void onCameraChange(CameraPosition position) {
|
||||
Log.v(MapboxConstants.TAG, position.toString());
|
||||
}
|
||||
});
|
||||
|
||||
findViewById(R.id.makeSnapshotButton).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
final long startTime = System.nanoTime();
|
||||
mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() {
|
||||
@Override
|
||||
public void onSnapshotReady(Bitmap snapshot) {
|
||||
long endTime = System.nanoTime();
|
||||
long duration = (long) ((endTime - startTime) / 1e6);
|
||||
ImageView snapshotView = (ImageView) findViewById(R.id.snapshotImageView);
|
||||
snapshotView.setImageBitmap(snapshot);
|
||||
Toast.makeText(SnapshotActivity.this, String.format("Snapshot taken in %d ms", duration), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mMapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mMapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mMapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mMapView.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
mMapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:background="@color/primary"
|
||||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@+id/toolbar"
|
||||
android:orientation="vertical" >
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@+id/cameraMapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
<ImageView
|
||||
android:id="@+id/snapshotImageView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/button_make_snapshot"
|
||||
android:id="@+id/makeSnapshotButton"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:background="@color/white"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -33,6 +33,7 @@
|
||||
<string name="activity_geocoder">Geocoder</string>
|
||||
|
||||
<!-- Other -->
|
||||
<string name="activity_snapshot">Snapshot Activity</string>
|
||||
<string name="activity_user_tracking_mode">User tracking mode</string>
|
||||
<string name="activity_custom_layer">Custom Layer (experimental)</string>
|
||||
<string name="activity_map_padding">Map Padding</string>
|
||||
@@ -63,6 +64,7 @@
|
||||
<string name="description_directions">Example with Directions API</string>
|
||||
<string name="description_geocoder">Example with Geocoder API</string>
|
||||
<string name="description_scroll_by">Scroll with pixels in x,y direction</string>
|
||||
<string name="description_snapshot">Example to make a snapshot of the map</string>
|
||||
|
||||
<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>
|
||||
|
||||
@@ -80,6 +82,7 @@
|
||||
<string name="button_camera_move">Move</string>
|
||||
<string name="button_camera_ease">Ease</string>
|
||||
<string name="button_camera_animate">Animate</string>
|
||||
<string name="button_make_snapshot">Snapshot</string>
|
||||
|
||||
<string name="label_fps">FPS:</string>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user