gpt4 book ai didi

google-maps - Android 中 Google map 标记周围的脉冲环动画

转载 作者:行者123 更新时间:2023-12-03 03:14:47 25 4
gpt4 key购买 nike

我想在 Android google mapFragment 中的蓝点当前用户位置上添加脉冲环动画(例如 Uber)。

有人可以帮我解决这个问题吗?

最佳答案

我找到了向标记添加脉动动画的解决方案。这是 map 部分,这里变量“map”表示您的 map 。

private Circle lastUserCircle;
private long pulseDuration = 1000;
private ValueAnimator lastPulseAnimator;

private void addPulsatingEffect(LatLng userLatlng){
if(lastPulseAnimator != null){
lastPulseAnimator.cancel();
Log.d("onLocationUpdated: ","cancelled" );
}
if(lastUserCircle != null)
lastUserCircle.setCenter(userLatlng);
lastPulseAnimator = valueAnimate(userLocation.getAccuracy(), pulseDuration, new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if(lastUserCircle != null)
lastUserCircle.setRadius((Float) animation.getAnimatedValue());
else {
lastUserCircle = map.addCircle(new CircleOptions()
.center(userLatlng)
.radius((Float) animation.getAnimatedValue())
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
}
}
});

}
protected ValueAnimator valueAnimate(float accuracy,long duration, ValueAnimator.AnimatorUpdateListener updateListener){
Log.d( "valueAnimate: ", "called");
ValueAnimator va = ValueAnimator.ofFloat(0,accuracy);
va.setDuration(duration);
va.addUpdateListener(updateListener);
va.setRepeatCount(ValueAnimator.INFINITE);
va.setRepeatMode(ValueAnimator.RESTART);

va.start();
return va;
}

您必须通过添加 PositionChangedListener 在位置更新时调用此方法。您可以在 Google map 文档中轻松找到它。之后,每次更新都调用上述方法。

将脉冲半径固定为大致相同的值,使其既不太大也不太小

编写这个方法

protected float getDisplayPulseRadius(float radius) {
float diff = (map.getMaxZoomLevel() - map.getCameraPosition().zoom);
if (diff < 3)
return radius;
if (diff < 3.7)
return radius * (diff / 2);
if (diff < 4.5)
return (radius * diff);
if (diff < 5.5)
return (radius * diff) * 1.5f;
if (diff < 7)
return (radius * diff) * 2f;
if (diff < 7.8)
return (radius * diff) * 3.5f;
if (diff < 8.5)
return (float) (radius * diff) * 5;
if (diff < 10)
return (radius * diff) * 10f;
if (diff < 12)
return (radius * diff) * 18f;
if (diff < 13)
return (radius * diff) * 28f;
if (diff < 16)
return (radius * diff) * 40f;
if (diff < 18)
return (radius * diff) * 60;
return (radius * diff) * 80;
}

并更改此行

userLocation.getAccuracy()

getDisplayPulseRadius(userLocation.getAccuracy()

还有

.radius((Float) animation.getAnimatedValue())

.radius(getDisplayPulseRadius((Float) animation.getAnimatedValue()))

如果您想要像颜色变大时颜色渐变为透明这样的效果,您可以在下一行设置动画器内的半径时使用它

circle.setFillColor(adjustAlpha(pulseAroundMeFillColor, 1 - animation.getAnimatedFraction()));

private int adjustAlpha(int color, float factor) {
int alpha = Math.round(Color.alpha(color) * factor);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}

关于google-maps - Android 中 Google map 标记周围的脉冲环动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com