[英]How to animate marker when it is added to map on Android?
我想在将地图标记添加到地图时为其设置动画。
用户应该看到带有标记的地图。 每个新标记都应该反弹。
你可以实现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
事件之外,您还可以在应用程序中添加标记时使用此功能。 我希望这只是你想要的。
您可以将任何新布局添加到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
将标记关闭屏幕或开始位置,然后开始动画。
请注意,此方法中使用的.setAnchor已于2013年5月添加到google map api v2中
我刚刚通过调整extras样本地图演示得到这个工作的一个标记,我不喜欢这个实现的性能。 最重要的部分是将标记悬挂在屏幕上或在起始位置关闭。 我正在使用屏幕上方。
将标记关闭屏幕.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;
}
祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.