How to animate marker on google maps ?

独自空忆成欢 提交于 2019-12-01 08:06:01
Andrii Omelchenko

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!