安卓动画未在onAnimationEnd中完成

Sha*_*are 65 animation android background drawable android-animation

onAnimationEnd虽然animation.hasEnded设置为true,但是当事件被触发时,似乎没有真正完成Android动画.

我希望我的视图可以改变它的背景可绘制的背景ScaleAnimation,但你可以清楚地看到它在完成之前已经改变了几毫秒.问题是,它会闪烁,因为新的背景会出现(=)缩放很短的时间,直到动画完成.

有没有办法获得动画的真实结束,或者只是阻止新背景在这么短的时间内缩放?

谢谢!


//编辑:我正在使用一个AnimationListener来接听以下电话:

    @Override
public void onAnimationEnd(Animation animation)
{
    View view = (MyView) ((ExtendedScaleAnimation) animation).getView();

    view.clearAnimation();
    view.requestLayout();
    view.refreshBackground(); // <-- this is where the background gets changed
}
Run Code Online (Sandbox Code Playgroud)

Soh*_*ham 91

以下是与此问题相关的实际错误http://code.google.com/p/android-misc-widgets/issues/detail?id=8

这基本上表明当AnimationListener附加到Animation时,onAnimationEnd方法不能很好地工作

解决方法是在要将动画应用到的视图中侦听动画事件.例如,如果最初将动画侦听器附加到动画上,就像这样

mAnimation.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation arg0) {
        //Functionality here
    }
});
Run Code Online (Sandbox Code Playgroud)

然后将动画应用到像这样的ImageView

mImageView.startAnimation(mAnimation);
Run Code Online (Sandbox Code Playgroud)

要解决此问题,您现在必须创建自定义ImageView

public class MyImageView extends ImageView {
Run Code Online (Sandbox Code Playgroud)

然后覆盖onAnimationEndView类的方法并提供其中的所有功能

@Override
protected void onAnimationEnd() {
    super.onAnimationEnd();
    //Functionality here
}
Run Code Online (Sandbox Code Playgroud)

这是此问题的正确解决方法,提供over-riden View - > onAnimationEnd方法中的功能,而不是附加到Animation的AnimationListener的onAnimationEnd方法.

这样可以正常工作,并且在动画结束时不再有任何闪烁.希望这可以帮助.

  • 我们无法再查看该链接中的错误,但这仍然是Android M中的问题,N ... (3认同)
  • 是的,我正在将动画推荐给imageView.我创建了视图类,但我从未进入onAnimationEnd (2认同)
  • 那么使用内容视图布局呢?在这种情况下,你不能使用嵌套类,因此不能使用父类功能,猜测. (2认同)

luc*_*cks 32

我通过在onAnimationEnd内部调用动画的视图上调用clearAnimation()来解决这个问题,它消除了闪烁它很奇怪为什么有人必须这样做,因为仅当动画已经结束时才应该调用onAnimationEnd回调.但我想答案在于Framework的深度如何处理视图/布局处理动画回调.现在把它作为一个无黑客的解决方案,只是工作.

        animation.setAnimationListener(new AnimationListener() {

        public void onAnimationEnd(Animation anim) {
            innerView.clearAnimation();   // to get rid of flicker at end of animation

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
            (innerBlockContainer.getWidth(), innerBlockContainer.getHeight());

            /* Update lp margin, left/top to update layout after end of Translation */
            ViewGroup parent_ofInnerView = (ViewGroup)innerView.getParent();
            vp.updateViewLayout(innerBlockContainer, lp);

        }

        public void onAnimationRepeat(Animation arg0) {}

        public void onAnimationStart(Animation arg0) {
        }

    });

     innerView.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)

  • 虽然调用**clearAnimation()**会再次调用**onAnimationEnd**! (4认同)

Doi*_*gen 6

我有类似的问题,我使用Soham的解决方案与自定义视图类.

它工作得很好,但最后,我发现了一个更简单的解决方案,对我有用.

在调用之后view.StartAnimation(animation),在我的程序中的下一步之前,我添加了一个短暂的延迟,该延迟将足以让动画完成,但是足够短以至于用户无法察觉:

new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {
           nextStepInMyProgram();
       }
     }, 200);// delay in milliseconds (200)
Run Code Online (Sandbox Code Playgroud)

  • 一个可怕的解决方案,但工作没有重写类,也只有post(Runnable),没有postDelayed(Runnable,int) (2认同)

sav*_*ion 5

我有同样的问题并使用它解决了

view.clearAnimation();
Run Code Online (Sandbox Code Playgroud)

之前

view.startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)