onAnimationEnd没有被调用,onAnimationStart工作正常

Kar*_*ran 46 animation android popupwindow

我在PopupWindow中有一个ScrollView.我正在使用TranslateAnimation为ScrollView内容设置动画.

动画启动时,会调用onAnimationStart侦听器,但不会调用onAnimationEnd.有任何想法吗 ?

布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@drawable/popup_window_bg"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
  <View
     android:layout_width="@dimen/toolbar_padding_left"
     android:layout_height="@dimen/toolbar_height"/>
  <ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+web/toolbar"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:scrollbars="none"
     android:visibility="invisible">
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">

       ...

    </LinearLayout>
  </ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

动画代码:

mToolbar = mPopupContents.findViewById( R.web.toolbar );
TranslateAnimation anim =
    new TranslateAnimation(0, 0, -60, 0);
anim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationStart(Animation a) {
            Log.d(LOGTAG, "---- animation start listener called"  );
        }
        public void onAnimationRepeat(Animation a) {}
        public void onAnimationEnd(Animation a) {
            Log.d(LOGTAG, "---- animation end listener called"  );
        }
    });
mToolbar.startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)

更新:我验证了onAnimationEnd被调用但是在一段延迟后被调用(假设您在此期间没有启动新动画).

Sho*_*use 58

AnimationEnd不可靠.如果您不想使用覆盖OnAnimationEnd的自定义视图重写代码,请使用postDelayed.

这是一些示例代码:

final FadeUpAnimation anim = new FadeUpAnimation(v);
anim.setInterpolator(new AccelerateInterpolator());
anim.setDuration(1000);
anim.setFillAfter(true);
new Handler().postDelayed(new Runnable() {
    public void run() {
        v.clearAnimation();
        //Extra work goes here
    }
}, anim.getDuration());
v.startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)

虽然看起来很难看,但我可以保证它非常可靠.我将它用于插入新行的ListViews,同时将动画移除到其他行.用AnimationEnd对一个监听器进行压力测试证明是不可靠的.有时AnimationEnd从未触发过.您可能希望postDelayed在动画未完全完成的情况下重新应用函数中的任何变换,但这实际上取决于您正在使用的动画类型.

  • 非常感谢你@ShortFuse.我正在努力将视图的可见性设置为`onAnimationStart()`中的`View.VISIBLE`,这对于某些Android版本不起作用.读完答案之后,我最终得到了:`workThatWouldGoInOnAnimationStart();``view.postDelayed(workThatWouldGoInOnAnimationEnd(),anim.getDuration());``view.startAnimation(anim);` (4认同)

Lis*_*tso 9

在我不记得帖子如何阅读以及花了多少时间找到这个问题的解决方案后,我发现如果要移动的对象不在屏幕区域上(例如位于屏幕坐标之外)OnAnimationEnd回调没有得到调用.可能是动画在启动后失败(调用start方法,我编写了一个监听器),但没有写入logcat.也许这不是你的情况,但这最终解决了我的问题,并希望它也可以帮助你.


ral*_*abb 9

确保您正在使用而 view.startAnimation(Animation) 不是 view.setAnimation(Animation).这种简单的混淆可能是一个问题.


net*_*ein -1

在您正在等待的动画结束之前,您是否不设置另一个动画?

如果没有上下文,很难理解是否是这样的……但这可能是唯一会导致这种情况的因素之一。