GIF marker animation for google map api ANDROID

I want to achieve marker animations, such as GIF animations. I got two images that should blink at the same time. I did not find anything that can be achieved in android. I am trying to do this by creating a handler that runs every 1 second, and I am trying to set the icon for the marker. But that does not work. Please guide me in the right direction.

my code currently looks like this.

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 does not work. Please help me what I am doing wrong.

+8
android google-maps google-maps-android-api-2 android-handler google-maps-markers
source share
1 answer

Please help me what I'm doing wrong

You are modifying an object that is no longer in use. When addMarker() is called, the addMarker() object MarkerOptions not make any difference, but this is what you are modifying with postDelayed() logic.

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

addMarker() returns a Marker . You will need to work with this Marker to influence your changes through setIcon() .

Also, since your bitmaps do not change, I suggest caching two BitmapDescriptor , rather than re-creating them in each pass.

+5
source share

All Articles