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

时间:2011-11-19 03:08:21

标签: android

我希望在将地图标记添加到地图时为其设置动画。

用户应该看到周围有标记的地图。每个新标记都应该反弹。

3 个答案:

答案 0 :(得分:13)

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

 private Marker mPerth;  
 private Marker mPerth = mMap.addMarker(new MarkerOptions()
             .position(PERTH)
            .title("Perth")
            .snippet("Population: 1,738,800"));        
  @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;
}

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

答案 1 :(得分:0)

您可以将任何新布局添加到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;
}

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

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

答案 2 :(得分:0)

将标记停在屏幕上或开始位置,然后开始动画。

请注意,此方法中使用的.setAnchor已于2013年5月添加到google map api v2

我刚刚通过调整extras samples maps demo来解决这个问题,我不喜欢这个实现的性能。最重要的部分是将标记悬挂在屏幕上或在起始位置关闭。我正在使用上面的屏幕。

将标记关闭屏幕.setAnchor(.5f,(标记上方的标记大小/标记大小)) //对于我的测试手机,地图演示珀斯大概是6f。更改动画反弹到相同的值我的测试手机是6f。

private void addMarkersToMap() {
    // A few more markers for good measure.
mPerth = mMap.addMarker(new MarkerOptions().position(PERTH)
            .title("Perth").snippet("Population: 1,738,800")
            .anchor(.5f, 6f)
            );

更改动画,使其反弹到(标记上方的屏幕大小/标记大小)(我的测试手机上的6f)。我只是使用onclick处理程序,因为它已经设置为反弹,调整反弹到6f和更长的持续时间。因此,在将所有标记添加到地图后,我将点击处理程序。

this.onMarkerClick(mPerth);

已更改的onMarkerClick处理程序具有6f且持续时间更长。

@Override
public boolean onMarkerClick(final Marker marker) {
    if (marker.equals(mPerth)) {
        // This causes the marker at Perth to bounce into position when it
        // is clicked.
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final long duration = 2500;

        final Interpolator interpolator = new BounceInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = Math.max(
                        1 - interpolator.getInterpolation((float) elapsed
                                / duration), 0);

                marker.setAnchor(0.5f, 1.0f + 6 * t);

                if (t > 0.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });
    } else if (marker.equals(mAdelaide)) {
        // This causes the marker at Adelaide to change color.
        marker.setIcon(BitmapDescriptorFactory.defaultMarker(new Random()
                .nextFloat() * 360));
    }
    // 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;
}

祝你好运