逐渐淡出自定义地图标记

时间:2015-01-23 12:08:22

标签: android google-maps-markers

我正在尝试逐渐淡出自定义Google地图标记。

我已经看过所有帖子说只是使用DevBytes视频中的代码掉落并用setAlpha替换setPosition,这是我试图做的。

问题在于,无论我做什么,我的图标在处理程序的持续时间内都会变为白色,然后在完成时变为透明,而不是逐渐淡化以完成透明度。

gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(final com.google.android.gms.maps.model.Marker marker) {
        if (marker.equals(myLocationMarker)) {

            final long duration = 1000;
            final int alpha = 100;
            final long start = SystemClock.uptimeMillis();
            final Handler handler = new Handler();
            final Interpolator interpolator = new LinearInterpolator();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = interpolator.getInterpolation((float) elapsed / duration);

                    float newAlpha = alpha - (t*100);
                    if(newAlpha<0)
                        newAlpha = 0;
                    int finalAlpha = (int)Math.ceil(newAlpha);
                    System.out.println("time = "+t);
                    System.out.println("newAlpha = "+newAlpha);
                    System.out.println("finalAlpha = "+finalAlpha);
                    marker.setAlpha(finalAlpha);

                    if (t < 1.0)
                        handler.postDelayed(this, 10);
                }
            });
            return true;
        }
    });

1 个答案:

答案 0 :(得分:7)

我尝试使用ValueAnimator并且它有效:

ValueAnimator ani = ValueAnimator.ofFloat(1, 0); //change for (0,1) if you want a fade in
ani.setDuration(5000);
ani.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        marker.setAlpha((float) animation.getAnimatedValue());
    }
});
ani.start();