Continue camera animation during gestures
This example demonstrates how to animate the pitch of the map using the Mapbox Maps SDK for Android.
The code below applies an animation to the MapView
by adjusting CameraAnimatorOptions
for example the start and end pitch values, animation duration, and interpolation. The animation repeats continuously, pausing for a second and then repeating. The animation continues to occur, even when a user gesture is activated due to the addProtectedAnimationOwner()
function.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.testapp.examples
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.plugin.animation.CameraAnimatorOptions
import com.mapbox.maps.plugin.animation.camera
import com.mapbox.maps.plugin.gestures.gestures
import java.util.concurrent.TimeUnit
class OngoingAnimationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapView = MapView(this)
setContentView(mapView)
mapView.mapboxMap
.apply {
setCamera(
CameraOptions.Builder()
.center(Point.fromLngLat(LONGITUDE, LATITUDE))
.zoom(9.0)
.build()
)
}
mapView.gestures.addProtectedAnimationOwner(OWNER)
val anim = mapView.camera.createPitchAnimator(
CameraAnimatorOptions.cameraAnimatorOptions(0.0, 30.0) {
owner(OWNER)
}
) {
repeatCount = ValueAnimator.INFINITE
repeatMode = ValueAnimator.REVERSE
duration = TimeUnit.SECONDS.toMillis(2)
interpolator = LinearInterpolator()
}
mapView.camera.registerAnimators(anim)
anim.start()
}
companion object {
private const val OWNER = "Example"
private const val LATITUDE = 40.0
private const val LONGITUDE = -74.5
}
}