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

Commit

Permalink
[android] - add additional Android Animator examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Sep 26, 2017
1 parent 2f4cc7e commit 521070b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import android.os.Bundle;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
Expand All @@ -24,6 +30,7 @@
public class CameraAnimatorActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final double ANIMATION_DELAY_FACTOR = 1.5;
private static final LatLng START_LAT_LNG = new LatLng(37.787947, -122.407432);

private MapView mapView;
private MapboxMap mapboxMap;
Expand All @@ -43,27 +50,42 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onMapReady(final MapboxMap map) {
mapboxMap = map;
initFab();
}

private void initFab() {
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setVisibility(View.GONE);
createAnimator(mapboxMap.getCameraPosition()).start();

CameraPosition animatedPosition = new CameraPosition.Builder()
.target(new LatLng(37.789992, -122.402214))
.tilt(60)
.zoom(14.5f)
.bearing(135)
.build();

createExampleAnimator(mapboxMap.getCameraPosition(), animatedPosition).start();
}
});
}

private Animator createAnimator(CameraPosition currentPosition) {
//
// Animator API used for the animation on the FAB
//

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

private Animator createLatLngAnimator(LatLng currentPosition) {
LatLng target = new LatLng(37.789992, -122.402214);
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new LatLngEvaluator(), currentPosition, target);
private Animator createLatLngAnimator(LatLng currentPosition, LatLng targetPosition) {
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new LatLngEvaluator(), currentPosition, targetPosition);
latLngAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
latLngAnimator.setInterpolator(new FastOutSlowInInterpolator());
latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -75,8 +97,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
return latLngAnimator;
}

private Animator createZoomAnimator(double currentZoom) {
ValueAnimator zoomAnimator = ValueAnimator.ofFloat((float) currentZoom, 14.5f);
private Animator createZoomAnimator(double currentZoom, double targetZoom) {
ValueAnimator zoomAnimator = ValueAnimator.ofFloat((float) currentZoom, (float) targetZoom);
zoomAnimator.setDuration((long) (2200 * ANIMATION_DELAY_FACTOR));
zoomAnimator.setStartDelay((long) (600 * ANIMATION_DELAY_FACTOR));
zoomAnimator.setInterpolator(new AnticipateOvershootInterpolator());
Expand All @@ -89,8 +111,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
return zoomAnimator;
}

private Animator createBearingAnimator(double currentBearing) {
ValueAnimator bearingAnimator = ValueAnimator.ofFloat((float) currentBearing, 135);
private Animator createBearingAnimator(double currentBearing, double targetBearing) {
ValueAnimator bearingAnimator = ValueAnimator.ofFloat((float) currentBearing, (float) targetBearing);
bearingAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
bearingAnimator.setStartDelay((long) (1000 * ANIMATION_DELAY_FACTOR));
bearingAnimator.setInterpolator(new FastOutLinearInInterpolator());
Expand All @@ -103,8 +125,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
return bearingAnimator;
}

private Animator createTiltAnimator(double currentTilt) {
ValueAnimator tiltAnimator = ValueAnimator.ofFloat((float) currentTilt, 60);
private Animator createTiltAnimator(double currentTilt, double targetTilt) {
ValueAnimator tiltAnimator = ValueAnimator.ofFloat((float) currentTilt, (float) targetTilt);
tiltAnimator.setDuration((long) (1000 * ANIMATION_DELAY_FACTOR));
tiltAnimator.setStartDelay((long) (1500 * ANIMATION_DELAY_FACTOR));
tiltAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -116,20 +138,82 @@ public void onAnimationUpdate(ValueAnimator animation) {
return tiltAnimator;
}

private static class LatLngEvaluator implements TypeEvaluator<LatLng> {
//
// Interpolator examples
//

private final LatLng latLng = new LatLng();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_animator, menu);
return true;
}

@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
public boolean onOptionsItemSelected(MenuItem item) {
if (mapboxMap == null) {
return false;
}
findViewById(R.id.fab).setVisibility(View.GONE);

// reset camera to init position
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(START_LAT_LNG)
.zoom(11)
.bearing(0)
.tilt(0)
.build()
));

// play animation
Animator animator = createExamepleInterpolator(item.getItemId());
if (animator != null) {
animator.start();
}

return super.onOptionsItemSelected(item);
}

private Animator createExamepleInterpolator(int menuItemId) {
switch (menuItemId) {
case R.id.menu_action_accelerate_decelerate_interpolator:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
createLatLngAnimator(START_LAT_LNG, new LatLng(37.826715, -122.422795)),
createExampleInterpolator(new FastOutSlowInInterpolator(), 2500));
return animatorSet;
case R.id.menu_action_bounce_interpolator:
AnimatorSet bounceAnimatorSet = new AnimatorSet();
bounceAnimatorSet.playTogether(
createLatLngAnimator(START_LAT_LNG, new LatLng(37.787947, -122.407432)),
createExampleInterpolator(new BounceInterpolator(), 3750)
);
return bounceAnimatorSet;
case R.id.menu_action_anticipate_overshoot_interpolator:
return createExampleInterpolator(new AnticipateOvershootInterpolator(), 2500);
case R.id.menu_action_path_interpolator:
return createExampleInterpolator(PathInterpolatorCompat.create(.22f, .68f, 0, 1.71f), 2500);
}
return null;
}

private Animator createExampleInterpolator(Interpolator interpolator, long duration) {
ValueAnimator zoomAnimator = ValueAnimator.ofFloat(11.0f, 16.0f);
zoomAnimator.setDuration((long) (duration * ANIMATION_DELAY_FACTOR));
zoomAnimator.setInterpolator(interpolator);
zoomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mapboxMap.setZoom((Float) animation.getAnimatedValue());
}
});
return zoomAnimator;
}

//
// MapView lifecycle
//

@Override
protected void onStart() {
super.onStart();
Expand Down Expand Up @@ -171,4 +255,21 @@ public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

/**
* Helper class to evaluate LatLng objects with a ValueAnimator
*/
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
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_cameraTargetLat="37.787947"
app:mapbox_cameraTargetLng="-122.407432"
app:mapbox_cameraZoom="11"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_action_accelerate_decelerate_interpolator"
android:title="@string/menuitem_title_accelerate_decelerate"
app:showAsAction="never"/>
<item
android:id="@+id/menu_action_bounce_interpolator"
android:title="@string/menuitem_title_bounce"
app:showAsAction="never"/>
<item
android:id="@+id/menu_action_anticipate_overshoot_interpolator"
android:title="@string/menuitem_title_anticipate_overshoot"
app:showAsAction="never"/>
<item
android:id="@+id/menu_action_path_interpolator"
android:title="@string/menuitem_title_path"
app:showAsAction="never"/>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<string name="menuitem_title_change_location_source_lost">Change to LOST location source</string>
<string name="menuitem_title_change_location_source_mock">Change to mock location source</string>
<string name="menuitem_title_change_location_source_null">Reset location source to null</string>
<string name="menuitem_title_accelerate_decelerate">Accelerate/Decelerate interpolator</string>
<string name="menuitem_title_bounce">Bounce interpolator</string>
<string name="menuitem_title_anticipate_overshoot">Anticipate/Overshoot interpolator</string>
<string name="menuitem_title_path">PathInterpolator interpolator</string>
<string name="button_camera_move">Move</string>
<string name="button_camera_ease">Ease</string>
<string name="button_camera_animate">Animate</string>
Expand Down

0 comments on commit 521070b

Please sign in to comment.