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

3
我正在尝试使用启动画面来覆盖谷歌地图加载延迟(适用于Maps v2)。
我想在FrameLayout中使用一个ImageView,在几秒钟后淡出,并能够重写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+)上实现这个的方法吗?

谢谢!


问题是,你将这个Runnable发布给哪个Handler,而且那个Handler做什么。如果那个Handler是你的主UI线程并且很忙,它将永远没有机会处理你的请求。 - class stacker
等一下,你正在将它发布到一个View上,因此你正在将它发布到主UI线程。那么你的主UI线程做什么? - class stacker
是的,我正在使用一个视图进行发布,正如我所说,请求正在处理,因为动画正在工作。唯一不起作用的是侦听器的'onAnimationEnd'。在界面线程上,我只显示一个SupportMapFragment。 - mirel.cocosila
你说得对。我没有完全理解你的问题。你确定通过调试/日志记录onAnimationEnd()根本没有被调用,还是因为你没有看到它的效果而得出结论它没有被调用? - class stacker
当然我很确定。我使用了调试器并注意到,如果我直接调用动画(而不是使用postDelayed),一切都可以正常工作 - 即使是onAnimationEnd - mirel.cocosila
3个回答

2

以下是我想要的结果的简单解决方法(即在显示ImageView几秒钟后将其淡出并设置为GONE):

因为postDelayed是我之前尝试的罪魁祸首,所以我放弃了它,转而使用AnimationsetStartOffset(startOffset)

这意味着我只需使用最初用于postDelayed的间隔延迟动画开始。我仍然不知道为什么postDelayed会干扰动画的监听器。


0

我有嵌套的LinearLayouts,其中一个是parentHolder,另一个是menuHolder,位于parentHolder内部。当触摸parent holder之外时,我会隐藏内部的那个。

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

}


0

可能还有人遇到了这个问题,即使阅读了很多stackoverflow的答案,也没有找到解决方法 - 就像我一样!

我的情况是:我使用了animatorSet,但是没有一个视图可以调用clearAnimation, 我没有从后台线程调用我的动画 - 顺便说一句,你永远不应该这样做 - 作为解决方案,我在animatorSet.start()之前调用了animatorSet.end()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接