Maps v2 marker animation - fade in and out
Asked Answered
C

4

5

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))
    );
Crispi answered 3/11, 2013 at 13:59 Comment(0)
R
3

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.

Rathe answered 3/11, 2013 at 14:17 Comment(0)
C
8

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();
Confide answered 18/7, 2017 at 9:37 Comment(1)
Tip: instead of using Animator.AnimatorListener, use AnimatorListenerAdapter, which allows you to only override onAnimationEnd and saves you of overriding 4 methods (3 of them empty).Insolvent
R
3

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.

Rathe answered 3/11, 2013 at 14:17 Comment(0)
A
0

use this code for fade in and out

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

            map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
Augustus answered 3/11, 2013 at 14:8 Comment(1)
Thanks, but I'm not sure you understand my question. I want to fade in the marker as it is being added to the map. This just zooms in the map at a custom durationCrispi
T
0

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


    }
Thyestes answered 7/12, 2016 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.