逐渐淡出自定义的地图标记 [英] Gradually fade out a custom, map marker
本文介绍了逐渐淡出自定义的地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试逐渐淡出自定义的Google地图标记.
I'm trying to gradually fade out a custom Google map marker.
我看过所有的帖子,说只使用DevBytes视频中的代码滴,然后将setPosition替换为setAlpha,这是我试图做的.
I've seen all the posts that say to just use the drop in code from the DevBytes video and replace the setPosition with setAlpha, which is what I have attempted to do.
问题是,无论我做什么,我的图标在处理程序期间都将变为白色,然后在完成时变为透明,而不是逐渐淡入淡出以实现完全透明.
The problem is that whatever I do, my icon just goes white for the duration of the handler and then transparent upon completion, instead of gradually fading to complete transparency.
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并成功:
I tried using ValueAnimator and it worked:
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();
这篇关于逐渐淡出自定义的地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文