【问题标题】:Cannot run multiple animations on GroundOverlay [GoogleMap]无法在 Ground Overlay [Google Map] 上运行多个动画
【发布时间】:2023-04-09 06:29:01
【问题描述】:

我试图在我的 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>

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我正在尝试实现与您提到的完全相同的动画。我使用您的代码作为参考,但我遇到了一些问题。我根本看不到动画。不知道我做错了什么。
    • 恐怕我不再为同一家公司工作,因此我无法再访问我使用的代码。这里的代码是我实现动画的唯一来源。不过,我记得它对我有用:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多