地图v2标记动画 - 淡入淡出

时间:2013-11-03 13:59:52

标签: android android-mapview android-animation android-maps-v2

如何淡出和淡出GoogleMap marker

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

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

4 个答案:

答案 0 :(得分:6)

更简单,更简洁的解决方案是使用Android SDK 11中引入的标准ObjectAnimator

渐弱是一个单行:

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();

答案 1 :(得分:3)

使用Google Play服务4.0+,您可以将Marker.setAlphaHandler结合使用,每隔几毫秒发布一些Runnable

代码与Drop marker slowly from top of screen to location on android map V2的答案类似。只需setAlpha代替setPosition,您就在回家的路上。

答案 2 :(得分:0)

使用此代码淡入淡出

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

            map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

答案 3 :(得分: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);
                        }
                    }
                });
            }
        });


    }