Put animated GIF in overlay over MapView

后端 未结 1 1416
南方客
南方客 2021-02-09 15:20

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:

1条回答
  •  终归单人心
    2021-02-09 15:27

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

    0 讨论(0)
提交回复
热议问题