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();
Animator.AnimatorListener
, useAnimatorListenerAdapter
, which allows you to only overrideonAnimationEnd
and saves you of overriding 4 methods (3 of them empty). – Insolvent