如何在Android上添加到地图时为标记设置动画?

Ale*_*rov 13 android

我想在将地图标记添加到地图时为其设置动画.

用户应该看到带有标记的地图.每个新标记都应该反弹.

GrI*_*sHu 13

您可以实施onMarkerClick()并在用户点击时使标记反弹.试试下面的代码实现.我尝试过它,它的工作完全正常.

 private Marker mPerth;  
 private Marker mPerth = mMap.addMarker(new MarkerOptions()
             .position(PERTH)
            .title("Perth")
            .snippet("Population: 1,738,800"));        
Run Code Online (Sandbox Code Playgroud)
  @Override   
public boolean onMarkerClick(final Marker marker) 
  {
      // This causes the marker at Perth to bounce into position when it is clicked.
    if (marker.equals(mPerth)) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = mMap.getProjection();
        Point startPoint = proj.toScreenLocation(PERTH);
        startPoint.offset(0, -100);
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        final long duration = 1500;
        final Interpolator interpolator = new BounceInterpolator();
        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);
                double lng = t * PERTH.longitude + (1 - t) * startLatLng.longitude;
                double lat = t * PERTH.latitude + (1 - t) * startLatLng.latitude;
                marker.setPosition(new LatLng(lat, lng));
                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });
    }
    // We return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false;
}
Run Code Online (Sandbox Code Playgroud)

除了onClick事件之外,您还可以在应用程序中添加标记时使用此功能.我希望这只是你想要的.


Arv*_*vis -1

您可以将任何新布局添加到 MapView 作为地图标记:

public void AddAnimMarkerToMap(MapView map, GeoPoint geoPoint, int id, int animResId)
{
    var layoutParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                                                ViewGroup.LayoutParams.WrapContent,
                                                geoPoint,
                                                MapView.LayoutParams.Center);

    var ll = new LinearLayout(map.Context) { Id = id, Orientation = Orientation.Vertical };
    ll.SetGravity(GravityFlags.Center);

    var iv = new ImageView(map.Context);
    iv.SetImageResource(animResId);

    ll.AddView(iv);
    map.AddView(ll, layoutParams);

    var markerAnimation = (AnimationDrawable)iv.Drawable;
    markerAnimation.Start();
    ll.LayoutParameters = layoutParams;
}
Run Code Online (Sandbox Code Playgroud)

也许你可以直接添加 ImageView 而无需包装布局。animResId 是 Frame 动画可绘制资源(类似于 Android Mylocation 标记)。

http://developer.android.com/guide/topics/resources/animation-resource.html#Frame