Put animated GIF in overlay over MapView

那年仲夏 提交于 2020-01-12 05:33:07

问题


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!


回答1:


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);
    
    }
    


来源:https://stackoverflow.com/questions/6052830/put-animated-gif-in-overlay-over-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!