[android] map snapshotter - add mutators

This commit is contained in:
Ivo van Dongen
2017-10-02 17:22:56 +03:00
committed by Fabian Guerra Soto
parent 31873725d7
commit fd3587608b
9 changed files with 210 additions and 4 deletions

View File

@@ -184,7 +184,7 @@ public class MapSnapshotter {
* Starts loading and rendering the snapshot. The callbacks will be fired
* on the calling thread.
*
* @param callback the callback to use when the snapshot is ready
* @param callback the callback to use when the snapshot is ready
* @param errorHandler the error handler to use on snapshot errors
*/
public void start(@NonNull MapboxMap.SnapshotReadyCallback callback, ErrorHandler errorHandler) {
@@ -197,12 +197,42 @@ public class MapSnapshotter {
nativeStart();
}
/**
* Updates the snapshotter with a new size
*
* @param width the width
* @param height the height
*/
public native void setSize(int width, int height);
/**
* Updates the snapshotter with a new {@link CameraPosition}
*
* @param cameraPosition the camera position
*/
public native void setCameraPosition(CameraPosition cameraPosition);
/**
* Updates the snapshotter with a new {@link LatLngBounds}
*
* @param region the region
*/
public native void setRegion(LatLngBounds region);
/**
* Updates the snapshotter with a new style url
*
* @param styleUrl the style url
*/
public native void setStyleUrl(String styleUrl);
/**
* Must be called in on the thread
* the object was created on.
*/
public void cancel() {
callback = null;
reset();
nativeCancel();
}

View File

@@ -377,6 +377,16 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterReuseActivity"
android:description="@string/description_map_snapshotter_reuse"
android:label="@string/activity_map_snapshotter_reuse">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.maplayout.DoubleMapActivity"
android:description="@string/description_doublemap"

View File

@@ -0,0 +1,110 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;
import java.util.Random;
/**
* Test activity showing how to use a the {@link MapSnapshotter}
*/
public class MapSnapshotterReuseActivity extends AppCompatActivity implements MapboxMap.SnapshotReadyCallback {
private MapSnapshotter mapSnapshotter;
private View fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_snapshotter_reuse);
fab = findViewById(R.id.fab);
fab.setVisibility(View.INVISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fab.setVisibility(View.INVISIBLE);
mapSnapshotter.setStyleUrl(getRandomStyle());
if (random.nextInt(2) == 0) {
mapSnapshotter.setCameraPosition(getRandomCameraPosition());
} else {
mapSnapshotter.setRegion(getRandomBounds());
}
if (random.nextInt(2) == 0) {
mapSnapshotter.setSize(512, 512);
} else {
mapSnapshotter.setSize(256, 256);
}
mapSnapshotter.start(MapSnapshotterReuseActivity.this);
}
});
mapSnapshotter = new MapSnapshotter(
getApplicationContext(),
new MapSnapshotter.Options(512, 512)
);
mapSnapshotter.start(MapSnapshotterReuseActivity.this);
}
@Override
public void onSnapshotReady(Bitmap snapshot) {
fab.setVisibility(View.VISIBLE);
ImageView imageView = (ImageView) findViewById(R.id.snapshot_image);
imageView.setImageBitmap(snapshot);
}
private LatLngBounds getRandomBounds() {
return LatLngBounds.from(
randomInRange(-5, 5),
randomInRange(-5, 5),
randomInRange(5, 10),
randomInRange(5, 10)
);
}
private CameraPosition getRandomCameraPosition() {
return new CameraPosition.Builder()
.target(new LatLng(randomInRange(-80, 80), randomInRange(-160, 160)))
.zoom(randomInRange(2, 10))
.bearing(randomInRange(0, 90))
.build();
}
public String getRandomStyle() {
switch (random.nextInt(5)) {
case 0:
return Style.DARK;
case 1:
return Style.LIGHT;
case 2:
return Style.MAPBOX_STREETS;
case 3:
return Style.OUTDOORS;
case 4:
return Style.SATELLITE_STREETS;
default:
return Style.TRAFFIC_DAY;
}
}
private static Random random = new Random();
public static float randomInRange(float min, float max) {
return (random.nextFloat() * (max - min)) + min;
}
}

View File

@@ -0,0 +1,24 @@
<?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">
<ImageView
android:id="@+id/snapshot_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription=""/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_animate_coordinates"/>
</RelativeLayout>

View File

@@ -60,6 +60,7 @@
<string name="description_animated_image_source">Shows how to animate georeferenced images</string>
<string name="description_bottom_sheet">Show 2 MapView on screen with a bottom sheet</string>
<string name="description_map_snapshotter">Show a static bitmap taken with the MapSnapshotter</string>
<string name="description_map_snapshotter_reuse">Show how to reuse a MapSnapshotter instance</string>
<string name="description_camera_animator">Use Android SDK Animators to animate camera position changes</string>
<string name="description_symbol_generator">Use Android SDK Views as symbols</string>
</resources>

View File

@@ -60,6 +60,7 @@
<string name="activity_animated_image_source">Animated Image Source</string>
<string name="activity_bottom_sheet">Bottom sheet</string>
<string name="activity_map_snapshotter">Map Snapshotter</string>
<string name="activity_map_snapshotter_reuse">Map Snapshotter Reuse</string>
<string name="activity_camera_animator">Animator animation</string>
<string name="activity_symbol_generator">SymbolGenerator</string>
</resources>

View File

@@ -81,9 +81,26 @@ void MapSnapshotter::start(JNIEnv&) {
void MapSnapshotter::cancel(JNIEnv&) {
MBGL_VERIFY_THREAD(tid);
snapshotCallback.reset();
snapshotter.reset();
}
void MapSnapshotter::setStyleUrl(JNIEnv& env, jni::String styleURL) {
snapshotter->setStyleURL(jni::Make<std::string>(env, styleURL));
}
void MapSnapshotter::setSize(JNIEnv&, jni::jint width, jni::jint height) {
auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
snapshotter->setSize(size);
}
void MapSnapshotter::setCameraPosition(JNIEnv& env, jni::Object<CameraPosition> position) {
auto options = CameraPosition::getCameraOptions(env, position);
snapshotter->setCameraOptions(options);
}
void MapSnapshotter::setRegion(JNIEnv& env, jni::Object<LatLngBounds> region) {
snapshotter->setRegion(LatLngBounds::getLatLngBounds(env, region));
}
// Static methods //
@@ -101,6 +118,10 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {
std::make_unique<MapSnapshotter, JNIEnv&, jni::Object<MapSnapshotter>, jni::Object<FileSource>, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::String>,
"nativeInitialize",
"finalize",
METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"),
METHOD(&MapSnapshotter::setSize, "setSize"),
METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"),
METHOD(&MapSnapshotter::setRegion, "setRegion"),
METHOD(&MapSnapshotter::start, "nativeStart"),
METHOD(&MapSnapshotter::cancel, "nativeCancel")
);

View File

@@ -40,6 +40,14 @@ public:
~MapSnapshotter();
void setStyleUrl(JNIEnv&, jni::String styleURL);
void setSize(JNIEnv&, jni::jint width, jni::jint height);
void setCameraPosition(JNIEnv&, jni::Object<CameraPosition> position);
void setRegion(JNIEnv&, jni::Object<LatLngBounds> region);
void start(JNIEnv&);
void cancel(JNIEnv&);

View File

@@ -76,6 +76,7 @@ std::string MapSnapshotter::Impl::getStyleURL() const {
void MapSnapshotter::Impl::setSize(Size size) {
map.setSize(size);
frontend.setSize(size);
}
Size MapSnapshotter::Impl::getSize() const {