这就是我向地图添加标记的方式:
marker = map.addMarker(new MarkerOptions()
.position(point)
.title(title)
.snippet(text)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
);
这就是我向地图添加标记的方式:
marker = map.addMarker(new MarkerOptions()
.position(point)
.title(title)
.snippet(text)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
);
一个更简单、更简洁的解决方案是仅使用ObjectAnimator
Android SDK 11 中引入的标准。
淡入实际上是一个单行:
ObjectAnimator.ofFloat(marker, "alpha", 0f, 1f).setDuration(500).start();
动画完成后,淡出需要更多代码才能正确删除标记:
Animator animator = ObjectAnimator.ofFloat(marker, "alpha", 1f, 0f);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
marker.remove();
}
@Override public void onAnimationStart(Animator animator) {}
@Override public void onAnimationCancel(Animator animator) {}
@Override public void onAnimationRepeat(Animator animator) {}
});
animator.setDuration(500).start();
使用 Google Play 服务 4.0+,您可以与每隔几毫秒发布的帖子Marker.setAlpha
结合使用。Handler
Runnable
该代码将类似于我在此处的答案Drop marker 从屏幕顶部慢慢地放置到 android map V2 上的位置。setAlpha
而不是,你setPosition
正在回家的路上。
使用此代码淡入淡出
map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
这是一个更完整的解决方案,专门针对添加标记时的淡入。需要注意的是requestNumber。如果您在移动地图时加载项目,这很有用。只需在每次服务调用时增加它或在不需要时将其删除。
public void fadeInMarker(Activity activity,final GoogleMap map, final MarkerOptions markerOptions, final long duration,final int requestNumber){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentRequestNumber != requestNumber){
return;
}
markerOptions.alpha(0);
final Marker marker = map.addMarker(markerOptions);
final AccelerateInterpolator accelartor = new AccelerateInterpolator();
final Long startTime = SystemClock.uptimeMillis();
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
float diff = SystemClock.uptimeMillis() - startTime;
float alpha = accelartor.getInterpolation(diff / duration);
if (alpha < 1) {
handler.postDelayed(this, 10);
}
else{
alpha = 1;
}
if (currentRequestNumber == requestNumber){
marker.setAlpha(alpha);
}
}
});
}
});
}