Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maps v2 marker animation - fade in and out

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))
    );
like image 607
Jakob Avatar asked Nov 03 '13 13:11

Jakob


2 Answers

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();
like image 141
pqvst Avatar answered Oct 03 '22 06:10

pqvst


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.

like image 43
MaciejGórski Avatar answered Oct 03 '22 07:10

MaciejGórski