Put animated GIF overlay on top of MapView

I pull out my hair, trying to get this seemingly simple task to work. I need to put an animated gif in an overlay on the map.

I have the following code:

AnimationDrawable anim = (AnimationDrawable)getResources().getDrawable(R.drawable.explosion); 

How do I now put this in the overlay and throw it on the card?

Currently for still images I have this:

 class DrawableIcon extends ItemizedOverlay { private ArrayList mOverlays = new ArrayList(); public DrawableIcon(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } }
class DrawableIcon extends ItemizedOverlay { private ArrayList mOverlays = new ArrayList(); public DrawableIcon(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } } 

which I then use as such: (gp is the geometry of the spot in which I want to put the image)

 DrawableIcon image = new DrawableIcon(this.getResources().getDrawable(ResourceID)); image.addOverlay(new OverlayItem(gp, "", "")); mapOverlays.add(image);
DrawableIcon image = new DrawableIcon(this.getResources().getDrawable(ResourceID)); image.addOverlay(new OverlayItem(gp, "", "")); mapOverlays.add(image); 

So, how can I change this code so that when the ResourceID is the identifier of the animated gif, the gif will play its animation over mapview?

Thanks in advance!

+6
source share
1 answer

To display animation on a map, you can use the addView method of the MapView class. But view layout options need to be initialized using MapView.LayoutParams.

In this case, the view will automatically move when you scroll the map!

It is as simple as:

  • just create a view (for example, ImageView or any other view);
  • initialize the layout of the view, create and pass MapView.LayoutParams to the setLayoutParams method;
  • Add a view to MapView.

     public static void addAnimationToMap(MapView map, int animationResourceId, GeoPoint geoPoint) { final ImageView view = new ImageView(map.getContext()); view.setImageResource(animationResourceId); //Post to start animation because it doesn't start if start() method is called in activity OnCreate method. view.post(new Runnable() { @Override public void run() { AnimationDrawable animationDrawable = (AnimationDrawable) view.getDrawable(); animationDrawable.start(); } }); map.addView(view); MapView.LayoutParams layoutParams = new MapView.LayoutParams( MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, geoPoint, MapView.LayoutParams.BOTTOM_CENTER); view.setLayoutParams(layoutParams); } 
+6
source

Source: https://habr.com/ru/post/888491/


All Articles