Skip to content

Commit

Permalink
fix: animation
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoso committed Jan 16, 2025
1 parent 9bdcc25 commit eba4a98
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class ClusterRendererMultipleItems<T extends ClusterItem> implements Clus
*/
private float mZoom;

private final ViewModifier mViewModifier = new ViewModifier();
private final ViewModifier mViewModifier = new ViewModifier(Looper.getMainLooper());

private ClusterManager.OnClusterClickListener<T> mClickListener;
private ClusterManager.OnClusterInfoWindowClickListener<T> mInfoWindowClickListener;
Expand Down Expand Up @@ -278,6 +278,9 @@ public void setMinClusterSize(int minClusterSize) {
*/
@SuppressLint("HandlerLeak")
private class ViewModifier extends Handler {
public ViewModifier(Looper looper) {
super(looper);
}
private static final int RUN_TASK = 0;
private static final int TASK_FINISHED = 1;
private boolean mViewModificationInProgress = false;
Expand Down Expand Up @@ -448,7 +451,6 @@ public void run() {
}
}

// Remove the old markers, animating them into clusters if zooming out.
for (final MarkerWithPosition marker : markersToRemove) {
boolean onScreen = visibleBounds.contains(marker.position);
if (onScreen && mAnimate) {
Expand All @@ -466,7 +468,9 @@ public void run() {
break;
}
}

}
// Remove it because it will join a cluster
markerModifier.animateThenRemove(marker, marker.position, foundItem.getPosition());
} else {
markerModifier.remove(true, marker.marker);
Expand Down Expand Up @@ -633,7 +637,6 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {

for (AnimationTask existingTask : ongoingAnimations) {
if (existingTask.marker.getId().equals(task.marker.getId())) {
System.out.println("RemovingRemoving");
existingTask.cancel();
break;
}
Expand All @@ -655,6 +658,14 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) {
lock.lock();
AnimationTask animationTask = new AnimationTask(marker, from, to);
for (AnimationTask existingTask : ongoingAnimations) {
if (existingTask.marker.getId().equals(animationTask.marker.getId())) {
existingTask.cancel();
break;
}
}

ongoingAnimations.add(animationTask);
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
mAnimationTasks.add(animationTask);
lock.unlock();
Expand Down Expand Up @@ -1136,7 +1147,14 @@ public void perform() {
}

public void cancel() {
if (Looper.myLooper() != Looper.getMainLooper()) {
new Handler(Looper.getMainLooper()).post(this::cancel);
return;
}
markerWithPosition.position = to;
mRemoveOnComplete = false;
valueAnimator.cancel();
ongoingAnimations.remove(this);
}

@Override
Expand Down Expand Up @@ -1173,6 +1191,7 @@ public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
double lng = lngDelta * fraction + from.longitude;
LatLng position = new LatLng(lat, lng);
marker.setPosition(position);
markerWithPosition.position = position;
}
}
}

0 comments on commit eba4a98

Please sign in to comment.