在Android中的Google地图标记周围圈选动画 [英] Pulse ring animation around a Google Maps marker in Android

查看:130
本文介绍了在Android中的Google地图标记周围圈选动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何人都可以帮助我解决这个问题吗?

我想在Android google google mapFragment(如Uber)中添加一个脉冲环动画。 / p>

解决方案

我找到了一种将脉动动画添加到标记的解决方案。
这里是地图部分,这里的变量map表示你的地图。

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

private void addPulsatingEffect(Latlng userLatlng){
if(lastPulseAnimator!= null){
lastPulseAnimator.cancel();
Log.d(onLocationUpdated:,取消);
}
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((浮动)animation.getAnimatedValue())
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
}
}
});

$ b保护ValueAnimator valueAnimate(float精度,持续时间长,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地图文档中轻松找到该文件。之后,每次更新都会调用上述方法。

将脉冲半径固定为相同的值,这样既不会太大也不会太小<



保护浮动getDisplayPulseRadius(浮动半径){
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);
返回Color.argb(alpha,red,green,blue);
}


I want to add a pulse ring animation on blue dot current user location in Android google mapFragment (like Uber).

Can anybody help me with this thing?

解决方案

I have found a solution to add pulsating animation to a marker. Here is the map part, Here variable "map" denotes your 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;
    }

You have to call this on location updates by adding PositionChangedListener. You can easily find that in Google map docs. After that, for each update call the above method.

Fixing the pulse radius to somewhat same value, so that it's neither too big, nor too small

Write this method

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

And change this line

userLocation.getAccuracy()

to

getDisplayPulseRadius(userLocation.getAccuracy()

And also

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

to

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

If you want an effect like the color fades to transparent when it gets big, you can use this just in the next line where you are setting the radius inside the animator

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

这篇关于在Android中的Google地图标记周围圈选动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆