当使用postDelayed时,不会触发AnimationListener的onAnimationEnd

时间:2013-01-31 09:33:19

标签: android animation postdelayed

我正在尝试使用启动画面来覆盖Google地图加载延迟(适用于地图v2)。

我想在ImageView中使用FrameLayout,该内容应在几秒后淡出,并且能够覆盖onAnimationEnd以隐藏启动画面的ImageView

在没有延迟的情况下开始动画时,正在调用onAnimationEnd

new Animations().animateAlphaForLayout(splashLayout, 2000);

问题是,当我尝试使用postDelayed启动动画时,onAnimationEnd根本没有被调用:

splashLayout.postDelayed(new Runnable() {

    @Override
    public void run() {
        new Animations().animateAlphaForLayout(splashLayout, 2000);
    }

}, 3000);

Animations类'代码是这样的:

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;

public class Animations {

    public void animateAlphaForLayout(final ViewGroup viewGroup, int duration) {
        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(1.0f, 0.0f);
        animation.setRepeatCount(0);
        animation.setFillAfter(true);
        animation.setDuration(duration);
        animation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewGroup.setVisibility(View.GONE);
            }
        });
        set.addAnimation(animation);

        LayoutAnimationController controller = new LayoutAnimationController(set, 0.0f);
        viewGroup.setLayoutAnimation(controller);
        }

}

有没有人知道如何为Android 2.3及更高版本(API 10+)执行此操作?

谢谢!

3 个答案:

答案 0 :(得分:2)

一个简单的解决方法,可以获得我想要的结果(即显示ImageView几秒钟,然后淡出并将其设置为GONE):

由于postDelayed是我之前尝试的罪魁祸首,我放弃了它,转而使用Animation的{​​{1}}。

这意味着我只是以我最初用于setStartOffset(startOffset)的间隔延迟动画开始。我仍然不知道为什么postDelayed弄乱了动画的监听器。

答案 1 :(得分:0)

我嵌套了LinearLayouts,其中一个是parentHolder,另一个是parentHolder中的menuHolder。当在父母持有人之外触摸时,我隐藏了内在的那个。

package com.fmc.component;

import com.fmc.marketing.tanitim.R;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;

public class NavigatorTest {

public void AnimeEt(LinearLayout llParentHolder) {
    final LinearLayout llHolder = (LinearLayout) llParentHolder.findViewById(R.id.llHolder);
    AnimationSet aset = new AnimationSet(true);
    AlphaAnimation alpha = new AlphaAnimation(1, 0);
    alpha.setStartOffset(500);
    alpha.setDuration(1000);
    aset.addAnimation(alpha);
    alpha.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            llHolder.setVisibility(LinearLayout.GONE);
        }
    });
    llHolder.startAnimation(aset);
}

}

答案 2 :(得分:0)

可能有人仍然遇到此问题并且找不到解决方案 - 尽管读取了很多stackoverflow答案 - 就像我一样!

所以我的情况是:我使用了animatorSet和

我没有一个可以调用clearAnimation的视图, 我没有从backgroundThread调用我的动画 - 你永远不应该这样做,顺便说一句 作为一个解决方案,我确实在animatorSet.start()之前调用了animatorSet.end()