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

Commit

Permalink
[android] - add API to perform platform side animations
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Sep 15, 2017
1 parent c795d96 commit 63f4390
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,9 @@ public double getMinZoomLevel() {
* Sets the maximum zoom level the map can be displayed at.
* </p>
* <p>
* The default maximum zoomn level is 22. The upper bound for this value is 25.5.
* The default maximum zoomn level is 22. The upper bound for this value is 25.5.
* </p>
*
* @param maxZoom The new maximum zoom level.
*/
public void setMaxZoomPreference(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM,
Expand Down Expand Up @@ -610,6 +611,46 @@ public Light getLight() {
// Camera API
//

/**
* Moves the center of the screen to a latitude and longitude specified by a LatLng object. This centers the
* camera on the LatLng object.
*
* @param latLng Target location to change to
*/
public void setLatLng(@NonNull LatLng latLng) {
nativeMapView.setLatLng(latLng);
}

/**
* Moves the camera viewpoint to a particular zoom level.
*
* @param zoom Zoom level to change to
*/
public void setZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double zoom) {
//TODO use actual focal point
PointF pointF = new PointF(nativeMapView.getWidth() / 2, nativeMapView.getHeight() / 2);
nativeMapView.setZoom(zoom, pointF, 0);
}

/**
* Moves the camera viewpoint angle to a particular angle in degrees.
*
* @param tilt Tilt angle to change to
*/
public void setTilt(@FloatRange(from = MapboxConstants.MINIMUM_TILT, to = MapboxConstants.MAXIMUM_TILT) double tilt) {
nativeMapView.setPitch(tilt, 0);
}

/**
* Moves the camera viewpoint direction to a particular angle in degrees.
*
* @param bearing Direction angle to change to
*/
public void setBearing(@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION, to = MapboxConstants.MAXIMUM_DIRECTION)
double bearing) {
nativeMapView.setBearing(bearing);
}

/**
* Cancels ongoing animations.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.camera.CameraAnimatorActivity"
android:description="@string/description_camera_animator"
android:label="@string/activity_camera_animator">
<meta-data
android:name="@string/category"
android:value="@string/category_camera"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.camera.CameraPositionActivity"
android:description="@string/description_cameraposition"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.mapbox.mapboxsdk.testapp.activity.camera;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;

import com.mapbox.mapboxsdk.camera.CameraPosition;
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;

/**
* Test activity showcasing using Android SDK animators to animate camera position changes.
*/
public class CameraAnimatorActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final double ANIMATION_DELAY_FACTOR = 1.5;

private MapView mapView;
private MapboxMap mapboxMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_animator);

mapView = (MapView) findViewById(R.id.mapView);
if (mapView != null) {
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
}

@Override
public void onMapReady(final MapboxMap map) {
mapboxMap = map;
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setVisibility(View.GONE);
createAnimator(mapboxMap.getCameraPosition()).start();
}
});
}

private Animator createAnimator(CameraPosition currentPosition) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(createLatLngAnimator(currentPosition.target));
animatorSet.play(createZoomAnimator(currentPosition.zoom));
animatorSet.play(createBearingAnimator(currentPosition.bearing));
animatorSet.play(createTiltAnimator(currentPosition.tilt));
return animatorSet;
}

private Animator createLatLngAnimator(LatLng currentPosition) {
LatLng target = new LatLng(37.789992, -122.402214);
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new LatLngEvaluator(), currentPosition, target);
latLngAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
latLngAnimator.setInterpolator(new FastOutSlowInInterpolator());
latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mapboxMap.setLatLng((LatLng) animation.getAnimatedValue());
}
});
return latLngAnimator;
}

private Animator createZoomAnimator(double currentZoom) {
ValueAnimator zoomAnimator = ValueAnimator.ofFloat((float) currentZoom, 14.5f);
zoomAnimator.setDuration((long) (2200 * ANIMATION_DELAY_FACTOR));
zoomAnimator.setStartDelay((long) (600 * ANIMATION_DELAY_FACTOR));
zoomAnimator.setInterpolator(new AnticipateOvershootInterpolator());
zoomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mapboxMap.setZoom((Float) animation.getAnimatedValue());
}
});
return zoomAnimator;
}

private Animator createBearingAnimator(double currentBearing) {
ValueAnimator bearingAnimator = ValueAnimator.ofFloat((float) currentBearing, 135);
bearingAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
bearingAnimator.setStartDelay((long) (1000 * ANIMATION_DELAY_FACTOR));
bearingAnimator.setInterpolator(new FastOutLinearInInterpolator());
bearingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mapboxMap.setBearing((Float) animation.getAnimatedValue());
}
});
return bearingAnimator;
}

private Animator createTiltAnimator(double currentTilt) {
ValueAnimator tiltAnimator = ValueAnimator.ofFloat((float) currentTilt, 60);
tiltAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
tiltAnimator.setStartDelay((long) (1500 * ANIMATION_DELAY_FACTOR));
tiltAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mapboxMap.setTilt((Float) animation.getAnimatedValue());
}
});
return tiltAnimator;
}

private static class LatLngEvaluator implements TypeEvaluator<LatLng> {

private final LatLng latLng = new LatLng();

@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
latLng.setLatitude(startValue.getLatitude()
+ ((endValue.getLatitude() - startValue.getLatitude()) * fraction));
latLng.setLongitude(startValue.getLongitude()
+ ((endValue.getLongitude() - startValue.getLongitude()) * fraction));
return latLng;
}
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M8,5v14l11,-7z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="37.774913"
app:mapbox_cameraTargetLng="-122.419368"
app:mapbox_cameraZoom="11"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_play_arrow_black_24dp"
app:backgroundTint="@android:color/white"/>

</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="activity_infowindow_adapter">Custom InfoWindow</string>
<string name="activity_dynamic_infowindow_adapter">Custom Dynamic InfoWindow</string>
<string name="activity_camera_animation_types">Animation Types</string>
<string name="activity_camera_animator">Animator animation</string>
<string name="activity_camera_zoom">Zoom Methods</string>
<string name="activity_visible_coordinate_bounds">LatLngBounds Method</string>
<string name="activity_camera_position">CameraPosition Method</string>
Expand Down Expand Up @@ -83,6 +84,7 @@
<string name="description_info_window">Learn how to handle the InfoWindow</string>
<string name="description_add_bulk_markers">Add Markers In Bulk to a Map</string>
<string name="description_camera_animation_types">Showcase the different animation types</string>
<string name="description_camera_animator">Use Android SDK Animators to animate camera position changes</string>
<string name="description_visible_bounds">Center the camera around a bounds</string>
<string name="description_dynamic_marker">Update position and icon</string>
<string name="description_map_padding">Map Padding example</string>
Expand Down

0 comments on commit 63f4390

Please sign in to comment.