【问题标题】:How to animate marker on google maps ?如何在谷歌地图上制作动画标记?
【发布时间】:2016-10-31 16:06:34
【问题描述】:

我想要实现的目标应该很简单,但它不起作用。 我在我的地图上添加了一个标记,我正在尝试为它制作心跳动画。

我尝试了以下代码,但没有运气,

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

任何建议将不胜感激, 加上使用外部库也是一种选择,我只是没有找到。

【问题讨论】:

    标签: android google-maps marker objectanimator


    【解决方案1】:

    Cabezas answer 中很好地描述了一般方法。除了他的回答,对于您的任务,您应该将它应用于设置(根据Interpolator 为每一帧动画调整大小)位图标记。例如,您可以使用以下方法:

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

    其中 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);
        Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
        return bitmapResized;
    }
    

    调用是:

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

    marker 是您的标记,1000 - 一个脉冲持续 1 秒。

    【讨论】:

    • @andrji 是否可以将持续时间设置为 2 秒,它的工作时间是无限的..
    • @JhamanDas 最简单的方法是用if 语句“环绕”从float t =...handler.postDelayed(this, 16); 的行:if (elapsed < 2000) {。这意味着如果自 startTime 以来花费的时间少于 2 秒(2000 毫秒),我们需要调用动画的下一个“步骤”(handler.postDelayed(this, 16);)。否则 - 不是。
    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 2014-02-20
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多