5

如何淡入和淡出GoogleMap 标记

这就是我向地图添加标记的方式:

    marker = map.addMarker(new MarkerOptions()
            .position(point)
            .title(title)
            .snippet(text)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
    );
4

4 回答 4

6

一个更简单、更简洁的解决方案是仅使用ObjectAnimatorAndroid 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();
于 2017-07-18T09:37:36.707 回答
3

使用 Google Play 服务 4.0+,您可以与每隔几毫秒发布的帖子Marker.setAlpha结合使用。HandlerRunnable

该代码将类似于我在此处的答案Drop marker 从屏幕顶部慢慢地放置到 android map V2 上的位置setAlpha而不是,你setPosition正在回家的路上。

于 2013-11-03T14:17:14.100 回答
0

使用此代码淡入淡出

map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));

            map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
于 2013-11-03T14:08:03.187 回答
0

这是一个更完整的解决方案,专门针对添加标记时的淡入。需要注意的是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);
                        }
                    }
                });
            }
        });


    }
于 2016-12-07T12:21:24.597 回答