如何在Google地图上设置动画标记? [英] How to animate marker on google maps ?

查看:218
本文介绍了如何在Google地图上设置动画标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图达到的目标应该很简单,但它不起作用。
我在地图上添加了一个标记,并且正在尝试对其进行动画处理。



我已经尝试了下面的代码,但没有运气,

  ObjectAnimator pulse = ObjectAnimator.ofPropertyValuesHolder(userLocation,
PropertyValuesHolder.ofFloat(scaleX,2f),
PropertyValuesHolder.ofFloat(scaleY,2f)
);
pulse.setDuration(310);
pulse.setRepeatCount(ObjectAnimator.INFINITE);
pulse.setRepeatMode(ObjectAnimator.REVERSE);
pulse.start();

任何建议都会感激不尽,使用外部库的
加上也是一个选项,我只是没有找到一个。

解决方案

一般方法在中有详细介绍Cabezas答案。除了他的回答,对于您的任务您应该将其应用于标记的设置(根据 Interpolator 为每个动画帧调整大小)位图。例如,你可以使用像这样的方法来做到这点:

  private void pulseMarker(final Bitmap markerIcon,final Marker marker,final long onePulseDuration){
final Handler handler = new Handler();
final long startTime = System.currentTimeMillis();

最终插值器插值器=新CycleInterpolator(1f);
handler.post(new Runnable(){
@Override
public void run(){
long elapsed = System.currentTimeMillis() - startTime;
float t = interpolator.getInterpolation((float)elapsed / onePulseDuration);
marker.setIcon(bitmapDescriptorFactory.fromBitmap(scaleBitmap(markerIcon,1f + 0.05f * t)));
handler.postDelayed(this,16 );
}
});
}

其中16是一帧动画的持续时间, 1f + 0.05f * t - 标记图标大小增加和减少5%, scaleBitmap()为:

  public Bitmap scaleBitmap(Bitmap bitmap,float scaleFactor){
final int sizeX = Math.round(bitmap.getWidth()* scaleFactor);
final int sizeY = Math.round(bitmap.getHeight()* scaleFactor);
位图bitmapResized = Bitmap.createScaledBitmap(bitmap,sizeX,sizeY,false);
返回bitmapResized;
}

并呼叫:

 位图markerIcon = BitmapFactory.decodeResource(getResources(),R.drawable.ic_heart); 
pulseMarker(markerIcon,marker,1000);

其中 marker 是您的标记, 1000 - 一个脉冲的持续时间为1秒。

What i'm trying to achieve supposed to be very simple, yet it's not working. I have added a marker on my map, and i'm trying to animate it an heart beat.

I have tried the following code, but without no luck,

   ObjectAnimator pulse = ObjectAnimator.ofPropertyValuesHolder(userLocation,
            PropertyValuesHolder.ofFloat("scaleX",2f),
            PropertyValuesHolder.ofFloat("scaleY",2f)
            );
    pulse.setDuration(310);
    pulse.setRepeatCount(ObjectAnimator.INFINITE);
    pulse.setRepeatMode(ObjectAnimator.REVERSE);
    pulse.start();

any suggestion would be grateful, plus using an external library can be an option as well, I have just didn't find one.

解决方案

General approach well described in Cabezas answer. In addition to his answer, for your task You should apply it for setting (resized according Interpolator for each frame of animation) bitmap for marker. For example, You can do it by using method like this:

private void pulseMarker(final Bitmap markerIcon, final Marker marker, final long onePulseDuration) {
    final Handler handler = new Handler();
    final long startTime = System.currentTimeMillis();

    final Interpolator interpolator = new CycleInterpolator(1f);
    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = System.currentTimeMillis() - startTime;
            float t = interpolator.getInterpolation((float) elapsed / onePulseDuration);
            marker.setIcon(BitmapDescriptorFactory.fromBitmap(scaleBitmap(markerIcon, 1f + 0.05f * t)));
            handler.postDelayed(this, 16);
        }
    });
}

where 16 is duration of one frame of animation, 1f + 0.05f * t - is 5% increase and decrease of size of marker icon and scaleBitmap() is:

public Bitmap scaleBitmap(Bitmap bitmap, float scaleFactor) {
    final int sizeX = Math.round(bitmap.getWidth() * scaleFactor);
    final int sizeY = Math.round(bitmap.getHeight() * scaleFactor);
    Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
    return bitmapResized;
}

and call is:

Bitmap markerIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_heart);
pulseMarker(markerIcon, marker, 1000);

where marker is your marker, 1000 - 1 sec duration of one pulse.

这篇关于如何在Google地图上设置动画标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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