Skip to content

Commit 3ae9c77

Browse files
jushgithub-actions[bot]
authored andcommitted
Clean up CameraAnimationsPluginImpl.startHighLevelAnimation (#6580)
Previously we were calling `commitChanges` on animation end. That is not necessary anymore since we commit on every animation evaluation. It was done because previously we were coalescing the camera changes at Maps SDK level and there were some issues with user animators (see #597). Also, we were cleaning the `highLevelAnimatorSet` only if there was an animation listener provided. But it should be independent of that. I also took the opportunity to remove some unnecessary anonymous object creation: | Before | After | | -------- | ------- | | <img width="1413" height="1667" alt="Screenshot 2025-09-16 at 15 45 10" src="https://github.com/user-attachments/assets/2ddce143-04d6-49ad-9ee1-7080206e168a" /> | <img width="1413" height="1667" alt="Screenshot 2025-09-16 at 15 47 45" src="https://github.com/user-attachments/assets/3345a114-5670-448a-9519-fd4e157d92d5" /> | cc @mapbox/maps-android GitOrigin-RevId: 0daa7ff9f1920d9af5f57bf0435a7e122107e1eb
1 parent 25f4866 commit 3ae9c77

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

plugin-animation/src/main/java/com/mapbox/maps/plugin/animation/CameraAnimationsPluginImpl.kt

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,20 +389,21 @@ internal class CameraAnimationsPluginImpl : CameraAnimationsPlugin, MapCameraPlu
389389
}
390390

391391
private fun registerInternalUpdateListener(animator: CameraAnimator<*>) {
392-
animator.addInternalUpdateListener {
393-
postOnMainThread { onAnimationUpdateInternal(animator, it) }
394-
}
392+
// Since we have the `animator` we can constructor the update listener once here instead of
393+
// every time the animator is updated.
394+
val updateInternalFun = { onAnimationUpdateInternal(animator) }
395+
animator.addInternalUpdateListener { postOnMainThread(updateInternalFun) }
395396
}
396397

397-
private fun onAnimationUpdateInternal(animator: CameraAnimator<*>, valueAnimator: ValueAnimator) {
398+
private fun onAnimationUpdateInternal(animator: CameraAnimator<*>) {
398399
// add current animator to queue-set if was not present
399400
runningAnimatorsQueue.add(animator)
400401

401402
// set current animator value in any case
402403
updateCameraValue(animator, animator.animatedValue, cameraOptionsBuilder)
403404

404405
if (animator.type == CameraAnimatorType.ANCHOR) {
405-
anchor = valueAnimator.animatedValue as ScreenCoordinate
406+
anchor = animator.animatedValue as ScreenCoordinate
406407
}
407408

408409
// commit applies changes immediately
@@ -912,6 +913,19 @@ internal class CameraAnimationsPluginImpl : CameraAnimationsPlugin, MapCameraPlu
912913
}
913914
}
914915

916+
/**
917+
* Convenient property to clean up highLevelAnimatorSet listener on animation end.
918+
* This is reused to avoid recreating new listener each time.
919+
*/
920+
private val clearHighLevelAnimatorSetListener = object : AnimatorListenerAdapter() {
921+
override fun onAnimationEnd(animation: Animator) {
922+
// Make sure we only clean the current highLevelAnimatorSet
923+
if (highLevelAnimatorSet?.animatorSet === animation) {
924+
highLevelAnimatorSet = null
925+
}
926+
}
927+
}
928+
915929
private fun startHighLevelAnimation(
916930
animators: Array<CameraAnimator<*>>,
917931
animationOptions: MapAnimationOptions?,
@@ -941,23 +955,14 @@ internal class CameraAnimationsPluginImpl : CameraAnimationsPlugin, MapCameraPlu
941955
animationOptions?.interpolator?.let {
942956
interpolator = it
943957
}
944-
animatorListener?.let {
945-
// listeners in Android SDK use non thread safe lists
946-
postOnAnimatorThread {
947-
addListener(object : AnimatorListenerAdapter() {
948-
override fun onAnimationEnd(animation: Animator) {
949-
postOnMainThread {
950-
commitChanges()
951-
}
952-
953-
if (highLevelAnimatorSet?.animatorSet === animation) {
954-
highLevelAnimatorSet = null
955-
}
956-
}
957-
})
958+
// listeners in Android SDK use non thread safe lists
959+
postOnAnimatorThread {
960+
addListener(clearHighLevelAnimatorSetListener)
961+
animatorListener?.let {
958962
addListener(it)
959963
}
960964
}
965+
961966
playTogether(*animators)
962967
}
963968
animatorSet.calculateCameraAnimationHint(cameraAnimationHintFractions)?.let {

0 commit comments

Comments
 (0)