Question

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))
    );
Was it helpful?

Solution 2

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.

OTHER TIPS

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();

use this code for fade in and out

map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));

            map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

Here is a more complete solution specifically for Fade In when adding a marker. Something to note is the requestNumber. this is useful if you're loading items while moving the map. Just increment it on every service call or remove it if you don't need it.

public void fadeInMarker(Activity activity,final  GoogleMap map, final MarkerOptions markerOptions, final long duration,final int requestNumber){

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (currentRequestNumber != requestNumber){
                    return;
                }

                markerOptions.alpha(0);
                final Marker marker = map.addMarker(markerOptions);
                final AccelerateInterpolator accelartor = new AccelerateInterpolator();

                final Long startTime = SystemClock.uptimeMillis();

                final Handler handler = new Handler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        float diff = SystemClock.uptimeMillis() - startTime;
                        float alpha = accelartor.getInterpolation(diff / duration);
                        if (alpha < 1) {
                            handler.postDelayed(this, 10);
                        }
                        else{
                            alpha = 1;
                        }

                        if (currentRequestNumber == requestNumber){
                            marker.setAlpha(alpha);
                        }
                    }
                });
            }
        });


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top