GIF type animation for marker in google map api AN

2019-01-24 07:00发布

问题:

I want to achieve a marker animation such as GIF animation. I got two images which should be blinking simultaneously. I found nothing which can acheive this in android. I am trying to do is , creating a handler which run every 1 second , and I am trying to set icon for marker. But it doesnt work. Please guide me in right direction.

my code as of now is as follows.

  Handler handler = new Handler();
  Boolean marker_color_bool = true;

//adding marker and sending the marker instance to marker_animation() method where handler is called.

MarkerOptions marker = new MarkerOptions()
                .title(delivery_center_name)
                .snippet("This is the " + delivery_center_name + " location")
                .position(location)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.red_marker));

        google_map.addMarker(marker);

        marker_animation(marker);

marker_animation() method

 private final int ONE_SECONDS = 1000;
public void marker_animation(final MarkerOptions marker ) {

    handler.postDelayed(new Runnable() {
        public void run() {

            Log.e("running",""+marker_color_bool);

            if(marker_color_bool == true)
            {

                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_marker));
                marker_color_bool = false;
            }
            else
            {
                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.red_marker));
                marker_color_bool = true;
            }

            handler.postDelayed(this, ONE_SECONDS);
        }
    }, ONE_SECONDS);
}

this approach doesnt work..Please help me what I am doing wrong.

回答1:

Please help me what I am doing wrong

You are modifying an object that is no longer being used. Once addMarker() is called, the MarkerOptions object has no further meaning, yet this is what you are modifying via your postDelayed() logic.

(BTW, you don't need a Handler, as postDelayed() is available on any View)

addMarker() returns a Marker. You will need to work with that Marker to affect your changes, via setIcon().

Also, since your bitmaps are not changing, I suggest caching your two BitmapDescriptor objects, rather than re-creating them on every pass.