How to animate marker on google maps ?

前端 未结 1 1258
别跟我提以往
别跟我提以往 2021-01-14 07:46

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

相关标签:
1条回答
  • 2021-01-14 08:19

    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.

    0 讨论(0)
提交回复
热议问题