Skip to content

Commit eba4a98

Browse files
committed
fix: animation
1 parent 9bdcc25 commit eba4a98

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class ClusterRendererMultipleItems<T extends ClusterItem> implements Clus
124124
*/
125125
private float mZoom;
126126

127-
private final ViewModifier mViewModifier = new ViewModifier();
127+
private final ViewModifier mViewModifier = new ViewModifier(Looper.getMainLooper());
128128

129129
private ClusterManager.OnClusterClickListener<T> mClickListener;
130130
private ClusterManager.OnClusterInfoWindowClickListener<T> mInfoWindowClickListener;
@@ -278,6 +278,9 @@ public void setMinClusterSize(int minClusterSize) {
278278
*/
279279
@SuppressLint("HandlerLeak")
280280
private class ViewModifier extends Handler {
281+
public ViewModifier(Looper looper) {
282+
super(looper);
283+
}
281284
private static final int RUN_TASK = 0;
282285
private static final int TASK_FINISHED = 1;
283286
private boolean mViewModificationInProgress = false;
@@ -448,7 +451,6 @@ public void run() {
448451
}
449452
}
450453

451-
// Remove the old markers, animating them into clusters if zooming out.
452454
for (final MarkerWithPosition marker : markersToRemove) {
453455
boolean onScreen = visibleBounds.contains(marker.position);
454456
if (onScreen && mAnimate) {
@@ -466,7 +468,9 @@ public void run() {
466468
break;
467469
}
468470
}
471+
469472
}
473+
// Remove it because it will join a cluster
470474
markerModifier.animateThenRemove(marker, marker.position, foundItem.getPosition());
471475
} else {
472476
markerModifier.remove(true, marker.marker);
@@ -633,7 +637,6 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
633637

634638
for (AnimationTask existingTask : ongoingAnimations) {
635639
if (existingTask.marker.getId().equals(task.marker.getId())) {
636-
System.out.println("RemovingRemoving");
637640
existingTask.cancel();
638641
break;
639642
}
@@ -655,6 +658,14 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
655658
public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) {
656659
lock.lock();
657660
AnimationTask animationTask = new AnimationTask(marker, from, to);
661+
for (AnimationTask existingTask : ongoingAnimations) {
662+
if (existingTask.marker.getId().equals(animationTask.marker.getId())) {
663+
existingTask.cancel();
664+
break;
665+
}
666+
}
667+
668+
ongoingAnimations.add(animationTask);
658669
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
659670
mAnimationTasks.add(animationTask);
660671
lock.unlock();
@@ -1136,7 +1147,14 @@ public void perform() {
11361147
}
11371148

11381149
public void cancel() {
1150+
if (Looper.myLooper() != Looper.getMainLooper()) {
1151+
new Handler(Looper.getMainLooper()).post(this::cancel);
1152+
return;
1153+
}
1154+
markerWithPosition.position = to;
1155+
mRemoveOnComplete = false;
11391156
valueAnimator.cancel();
1157+
ongoingAnimations.remove(this);
11401158
}
11411159

11421160
@Override
@@ -1173,6 +1191,7 @@ public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
11731191
double lng = lngDelta * fraction + from.longitude;
11741192
LatLng position = new LatLng(lat, lng);
11751193
marker.setPosition(position);
1194+
markerWithPosition.position = position;
11761195
}
11771196
}
11781197
}

0 commit comments

Comments
 (0)