I've been pulling my hair out trying to get this seemingly simple task to work. I need to put an animated gif in an overlay on a mapview.
I have the following code:
AnimationDrawable anim = (AnimationDrawable)getResources().getDrawable(R.drawable.explosion);
However how do I now pop that in an overlay and throw it over the mapview?
Currently, to put static 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();
}
}
Which I then use as such: (gp is the geopoint of the spot I want to put the image in)
DrawableIcon image = new DrawableIcon(this.getResources().getDrawable(ResourceID));
image.addOverlay(new OverlayItem(gp, "", ""));
mapOverlays.add(image);
So how would I go about modifying this code so that when ResourceID is the ID of an animated gif image, the gif image would play it's animation over the mapview?
Thanks in advance!
To display an animation over map you can use addView method of MapView class. But layout params of the view must be initialized with MapView.LayoutParams.
In this case the View will automatically move when you scroll the map!
It is as simple as:
add the 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With