How would one fade in and out a GoogleMap marker?
This is how I add a marker to the Map:
marker = map.addMarker(new MarkerOptions()
.position(point)
.title(title)
.snippet(text)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
);
A much simpler and cleaner solution is to just use the standard ObjectAnimator
which was introduced in Android SDK 11.
Fading in is literally a one-liner:
ObjectAnimator.ofFloat(marker, "alpha", 0f, 1f).setDuration(500).start();
Fading out requires a bit more code to properly remove the marker once the animation completes:
Animator animator = ObjectAnimator.ofFloat(marker, "alpha", 1f, 0f);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
marker.remove();
}
@Override public void onAnimationStart(Animator animator) {}
@Override public void onAnimationCancel(Animator animator) {}
@Override public void onAnimationRepeat(Animator animator) {}
});
animator.setDuration(500).start();
With Google Play services 4.0+ you can use Marker.setAlpha
in conjunction with Handler
that posts some Runnable
every few milliseconds.
The code will be similar to my answer here Drop marker slowly from top of screen to location on android map V2. Just setAlpha
instead of setPosition
and you are on your way home.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With