Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put animated GIF in overlay over MapView

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!

like image 724
RomanP Avatar asked May 19 '11 01:05

RomanP


1 Answers

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:

  1. just create a view (like ImageView or any other View);
  2. initialize the view layout, create and pass MapView.LayoutParams to setLayoutParams method;
  3. 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);
    
    }
    
like image 59
Igor Zubchenok Avatar answered Oct 06 '22 01:10

Igor Zubchenok