逐渐淡出自定义地图标记

Gradually fade out a custom, map marker

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

我看到所有的帖子都说只使用 DevBytes 视频中的代码并将 setPosition 替换为 setAlpha,这正是我尝试做的。

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

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

我尝试使用 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();