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

Commit

Permalink
[android] #5299 - remove MarkerView x,y animation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jun 17, 2016
1 parent 8e9b158 commit 8c83af7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public class MarkerView extends Marker {

private boolean selected;

private LatLng targetPosition;
private boolean shouldAnimate;
private boolean isAnimationRunning;
private AnimatorSet set;

private long startTime;
private long duration;
private long remainingTime;
private AnimatorUtils.OnAnimationEndListener animationEndListener;

/**
* Publicly hidden default constructor
*/
Expand Down Expand Up @@ -354,82 +344,6 @@ public void setMapboxMap(MapboxMap mapboxMap) {
markerViewManager = mapboxMap.getMarkerViewManager();
}

@Override
public void setPosition(LatLng position) {
setPosition(position, 0);
}

public void setPosition(LatLng position, long duration) {
setPosition(position, duration, null);
}

public void setPosition(LatLng position, long duration, AnimatorUtils.OnAnimationEndListener animationEndListener) {
this.animationEndListener = animationEndListener;

if (duration <= 0) {
// update position instantly
super.setPosition(position);
if (markerViewManager != null) {
markerViewManager.update();
}
} else {
// animate using Android SDK animations
this.targetPosition = position;
if (markerViewManager != null) {
markerViewManager.animatePosition(this, duration);
}
}
}

boolean shouldAnimate() {
return shouldAnimate;
}

void setShouldAnimate(boolean animating) {
shouldAnimate = animating;
}

boolean isAnimating() {
return isAnimationRunning;
}

void setAnimating(boolean animationRunning) {
isAnimationRunning = animationRunning;
}

LatLng getTargetPosition() {
return targetPosition;
}

void setAnimation(AnimatorSet set) {
this.set = set;
}

AnimatorSet getAnimation() {
return set;
}

long getRemainingTime() {
long time = duration - (AnimationUtils.currentAnimationTimeMillis() - startTime);
return time > 0 ? time : 0;
}

void setDuration(long duration) {
this.duration = duration;
}

void setStartTime(long startTime) {
this.startTime = startTime;
}

AnimatorUtils.OnAnimationEndListener getAnimationEndListener() {
return animationEndListener;
}

PointF getScreenLocation(@NonNull View convertView) {
return new PointF(convertView.getX() + offsetX, convertView.getY() + offsetY);
}

/**
* Get the String representation of a MarkerView.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.mapbox.mapboxsdk.annotations;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
Expand Down Expand Up @@ -43,7 +37,6 @@ public class MarkerViewManager {
private long viewMarkerBoundsUpdateTime;
private MapboxMap.OnMarkerViewClickListener onMarkerViewClickListener;
private ImageMarkerViewAdapter defaultMarkerViewAdapter;
private CountDownTimer timer;

/**
* Creates an instance of MarkerViewManager.
Expand Down Expand Up @@ -120,57 +113,22 @@ public void update() {
for (final MarkerView marker : markerViewMap.keySet()) {
final View convertView = markerViewMap.get(marker);
if (convertView != null) {
if (marker.isAnimating() || !marker.shouldAnimate()) {
if (marker.isAnimating()) {
// cancel ongoing animations
marker.setAnimating(false);
AnimatorSet set = marker.getAnimation();
List<Animator> animations = set.getChildAnimations();
float x = (Float) ((ObjectAnimator) animations.get(0)).getAnimatedValue();
float y = (Float) ((ObjectAnimator) animations.get(1)).getAnimatedValue();
PointF pointF = new PointF(x + marker.getOffsetX(), y + marker.getOffsetY());
marker.setPosition(mapboxMap.getProjection().fromScreenLocation(pointF));
set.cancel();
}

// update position on map
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
if (marker.getOffsetX() == -1) {
int x = (int) (marker.getAnchorU() * convertView.getMeasuredWidth());
int y = (int) (marker.getAnchorV() * convertView.getMeasuredHeight());
marker.setOffsetX(x);
marker.setOffsetY(y);
}

convertView.setX(point.x - marker.getOffsetX());
convertView.setY(point.y - marker.getOffsetY());

// animate visibility
if (marker.isVisible() && convertView.getVisibility() == View.GONE) {
convertView.animate().cancel();
convertView.setAlpha(0);
AnimatorUtils.alpha(convertView, 1);
}
} else {
if (timer == null) {
timer = new CountDownTimer(50, 560) {
@Override
public void onTick(long millisUntilFinished) {
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
if (marker.getOffsetX() == -1) {
int x = (int) (marker.getAnchorU() * convertView.getMeasuredWidth());
int y = (int) (marker.getAnchorV() * convertView.getMeasuredHeight());
marker.setOffsetX(x);
marker.setOffsetY(y);
}

}
convertView.setX(point.x - marker.getOffsetX());
convertView.setY(point.y - marker.getOffsetY());

@Override
public void onFinish() {
animate(marker, convertView);
}
};
} else {
timer.cancel();
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
convertView.setX(point.x - marker.getOffsetX());
convertView.setY(point.y - marker.getOffsetY());
}
timer.start();
// animate visibility
if (marker.isVisible() && convertView.getVisibility() == View.GONE) {
convertView.animate().cancel();
convertView.setAlpha(0);
AnimatorUtils.alpha(convertView, 1);
}
}
}
Expand Down Expand Up @@ -283,23 +241,17 @@ public View getView(MarkerView marker) {
* @param marker the MarkerView to remove
*/
public void removeMarkerView(MarkerView marker) {
boolean isAnimating = marker.isAnimating();
final View viewHolder = markerViewMap.get(marker);
if (viewHolder != null && marker != null) {
for (final MapboxMap.MarkerViewAdapter<?> adapter : markerViewAdapters) {
if (adapter.getMarkerClass().equals(marker.getClass())) {
if (!isAnimating) {
if (adapter.prepareViewForReuse(marker, viewHolder)) {
adapter.releaseView(viewHolder);
}
if (adapter.prepareViewForReuse(marker, viewHolder)) {
adapter.releaseView(viewHolder);
}
}
}
}

if (!isAnimating) {
markerViewMap.remove(marker);
}
markerViewMap.remove(marker);
}

/**
Expand Down Expand Up @@ -373,7 +325,7 @@ public void invalidateViewMarkersInBounds() {
Iterator<MarkerView> iterator = markerViewMap.keySet().iterator();
while (iterator.hasNext()) {
MarkerView m = iterator.next();
if (!m.shouldAnimate() && !markers.contains(m)) {
if (!markers.contains(m)) {
// remove marker
convertView = markerViewMap.get(m);
for (MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) {
Expand Down Expand Up @@ -445,50 +397,6 @@ public void onClick(final View v) {
}
}

public void animatePosition(@NonNull MarkerView marker, long duration) {
marker.setDuration(duration);
marker.setStartTime(AnimationUtils.currentAnimationTimeMillis());
marker.setShouldAnimate(true);
animate(marker, markerViewMap.get(marker));
}

private void animate(final MarkerView marker, @Nullable View convertView) {
if (convertView != null) {
marker.setAnimating(true);
PointF screenLocation = mapboxMap.getProjection().toScreenLocation(marker.getTargetPosition());
PointF currentLocation = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
convertView.setX(currentLocation.x - marker.getOffsetX());
convertView.setY(currentLocation.y - marker.getOffsetY());
ObjectAnimator animatorX = ObjectAnimator.ofFloat(convertView, "x", screenLocation.x - marker.getOffsetX());
ObjectAnimator animatorY = ObjectAnimator.ofFloat(convertView, "y", screenLocation.y - marker.getOffsetY());
AnimatorSet set = new AnimatorSet();
set.playTogether(animatorX, animatorY);
marker.setAnimation(set);
set.setDuration(marker.getRemainingTime());
set.addListener(new AnimatorListenerAdapter() {

private boolean canceled;

@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
canceled = true;
}

@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (!canceled) {
marker.setShouldAnimate(false);
marker.setPosition(marker.getTargetPosition());
}
}
});
marker.setAnimation(set);
set.start();
}
}

/**
* Default MarkerViewAdapter used for base class of MarkerView to adapt a MarkerView to an ImageView
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* you must obtain one from the getMapAsync() method on a MapFragment or MapView that you have
* added to your application.
* <p>
* Note: Similar to a View object, a GoogleMap should only be read and modified from the main thread.
* Note: Similar to a View object, a MapboxMap should only be read and modified from the main thread.
* </p>
*/
public class MapboxMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
Expand All @@ -16,7 +17,6 @@
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand All @@ -30,8 +30,6 @@

public class AnimatedMarkerActivity extends AppCompatActivity {

private final static String LOG_TAG = "AnimatedMarkerActivity";

private MapView mMapView;
private MapboxMap mMapboxMap;

Expand Down

0 comments on commit 8c83af7

Please sign in to comment.