GroundOverlay [GoogleMap] 上不能 运行 多个动画

Cannot run multiple animations on GroundOverlay [GoogleMap]

我想在我的 MapFragment 上实现呼吸效果动画,但由于某些原因,我无法在地图上 运行 多个动画。我想要实现的是这样的:

并且圈子在逐渐扩大并逐渐消失。现在我的动画是这样做的:

这不是我想要的。当前代码(如果我取消注释带有处理程序的 2 个块)只执行一个动画,其余形状保持静止。编辑我发现发生了什么事 - "startAnimation" 方法覆盖了当前动画。现在我想知道是否有办法避免这种覆盖?有什么想法吗?:

这是我的代码:

   @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

//Add smallest image drawable overlay

        mDotMarkerBitmap = generateBitmapFromDrawable(R.drawable.circle_drawable);
        groundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap))
                .position(xDesign, 100));
        groundAnimation = new RadiusAnimation(groundOverlay);
        groundAnimation.setRepeatCount(Animation.INFINITE);
        groundAnimation.setRepeatMode(Animation.RESTART);
        groundAnimation.setDuration(2000);
        mapFragment.getView().startAnimation(groundAnimation); // MapView where i show my map
//TODO  Fix the animation tomorrow
//Add medium size image drawable overlay

//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                // your code here
//                mDotMarkerBitmap = generateBitmapFromDrawable(R.drawable.circle_drawable2);
//                groundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
//                        .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap))
//                        .position(xDesign, 100));
//                groundAnimation = new RadiusAnimation(groundOverlay);
//                groundAnimation.setRepeatCount(Animation.INFINITE);
//                groundAnimation.setRepeatMode(Animation.RESTART);
//                groundAnimation.setDuration(10000);
//                mapFragment.getView().startAnimation(groundAnimation); // MapView where i show my map
//            }
//        }, 1000/* 1sec delay */);

//
////Add smallest size image drawable overlay
//        new Handler().postDelayed(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                // your code here
//                mDotMarkerBitmap = generateBitmapFromDrawable(R.drawable.circle_drawable3);
//                groundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
//                        .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap))
//                        .position(xDesign, 100));
//                groundAnimation = new RadiusAnimation(groundOverlay);
//                groundAnimation.setRepeatCount(Animation.INFINITE);
//                groundAnimation.setRepeatMode(Animation.RESTART);
//                groundAnimation.setDuration(2000);
//                mapFragment.getView().startAnimation(groundAnimation); // MapView where i show my map
//            }
//        }, 1000/* 1sec delay */);



        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(xDesign, 15));
    }

circle_drawable.xml(circle_drawable2 和 3 除了宽度和高度值相同,它们更大 60 和 90dp 以及颜色 - 蓝色和红色):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#0093e8"/>
    <size
        android:width="30dp"
        android:height="30dp"/>
</shape>

半径动画class:

public class RadiusAnimation extends Animation {
    private GroundOverlay groundOverlay;

    public RadiusAnimation(GroundOverlay groundOverlay) {
        this.groundOverlay = groundOverlay;

    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        groundOverlay.setDimensions( (100 * interpolatedTime) );
        groundOverlay.setTransparency( interpolatedTime );

    }


    @Override
    public void initialize(int width, int height, int parentWidth,int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }
}

万一有人试图做类似的事情,我找到了解决方案。为了同时 运行 多个动画,我可以使用 AnimationSet。所以我删除了延迟单个动画的处理程序,但由于每个动画都覆盖了最后一个,所以它不起作用,我看到最后一个添加在 mapFragment 上播放。所以我改变了我的代码:

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


//Add smallest image drawable overlay

        Bitmap mDotMarkerBitmap1 = generateBitmapFromDrawable(R.drawable.circle_drawable);

        GroundOverlay groundOverlay1 = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap1))
                .position(xDesign, 100));

        groundAnimation = new RadiusAnimation(groundOverlay1);
        groundAnimation.setRepeatCount(Animation.INFINITE);
        groundAnimation.setRepeatMode(Animation.RESTART);
        //        groundAnimation.setDuration(700);
        breadingAnimations.addAnimation(groundAnimation);


        Bitmap mDotMarkerBitmap2 = generateBitmapFromDrawable(R.drawable.circle_drawable2);
        GroundOverlay groundOverlay2 = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap2))
                .position(xDesign, 100));

        Animation groundAnimation2 = new RadiusAnimation(groundOverlay2);
        groundAnimation2.setRepeatCount(Animation.INFINITE);
        groundAnimation2.setRepeatMode(Animation.RESTART);
        //        groundAnimation2.setDuration(900);
        breadingAnimations.addAnimation(groundAnimation2);


        Bitmap mDotMarkerBitmap3 = generateBitmapFromDrawable(R.drawable.circle_drawable3);
        GroundOverlay groundOverlay3 = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap3))
                .position(xDesign, 100));
        Animation groundAnimation3 = new RadiusAnimation(groundOverlay3);
        groundAnimation2.setRepeatCount(Animation.INFINITE);
        groundAnimation2.setRepeatMode(Animation.RESTART);
        //        groundAnimation2.setDuration(1100);
        breadingAnimations.addAnimation(groundAnimation3);

        mapFragment.getView().startAnimation(breadingAnimations); // start animation


        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(xDesign, 15));
    }