无法在GroundOverlay上运行nultuple动画[GoogleMap]

时间:2016-07-13 09:07:47

标签: android animation google-maps-markers google-maps-android-api-2 android-maps

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

enter image description here

圆圈逐渐扩大,逐渐消失。现在我的动画是这样做的:

enter image description here

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

enter image description here

这是我的代码:

   @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>

RadiusAnimation类:

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

1 个答案:

答案 0 :(得分:0)

如果有人试图做类似的事情,我找到了解决方案。为了同时运行多个动画,我可以使用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));
    }