onAnimationEnd是没有得到所谓的,onAnimationStart工作正常正常、工作、onAnimationEnd、onAnimationStart

2023-09-06 00:30:58 作者:余生请指教

我有一个滚动型的PopupWindow。我使用TranslateAnimation动画滚动型的内容。

I've a ScrollView in the PopupWindow. I'm animating ScrollView contents using TranslateAnimation.

在动画开始时,onAnimationStart监听器被调用,但onAnimationEnd是没有得到调用。任何想法?

When animation starts, the onAnimationStart listener is called but the onAnimationEnd is not getting called. Any ideas ?

布局:

<?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>

动画code:

Animation code :

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

更新:我验证了onAnimationEnd的调用,但它被称为后,一些延迟(只要你不开始在这段时间内新的动画)

Update : I verified that the onAnimationEnd is called but it is called after some delay (provided you don't start the new animation during that time).

推荐答案

AnimationEnd 是不可靠的。如果你不希望重写code与自定义视图重写OnAnimationEnd,使用 postDelayed

AnimationEnd is not reliable. If you don't want to rewrite your code with custom views that override OnAnimationEnd, use postDelayed.

下面是一些例子code:

Here's some example code:

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

尽管它可能看起来很丑,我可以保证这是非常可靠的。我用它被插入新行同时与动画其他行删除列表视图。压力测试与AnimationEnd一个监听器被证明不可靠的。有时 AnimationEnd 从未被触发。您可能需要重新在 postDelayed 功能的情况下,动画没有完全完成所有改造,但是这真的取决于什么类型的动画你使用。

While it MAY seem ugly, I can guarantee it's very reliable. I use it for ListViews that are inserting new rows while removing with animation to other rows. Stress testing a listener with AnimationEnd proved unreliable. Sometimes AnimationEnd was never triggered. You might want to reapply any transformation in the postDelayed function in case the animation didn't fully finish, but that really depends on what type of animation you're using.

 
精彩推荐
图片推荐