看来,当Android动画还没有真正完成 onAnimationEnd 事件虽然被解雇 animation.hasEnded 设置为true。

我希望我的观点改变它的背景,可以在它的末尾绘制 ScaleAnimation 它确实如此,但是您可以清楚地看到它在完成之前已更改了一些miliseconds。问题是,它会闪烁,因为新背景出现(= IS)短时间缩放,直到动画真正完成为止。

有没有办法获得动画的真实末端,或者只是阻止新的背景在短时间内扩展到蜜蜂?

谢谢!


//编辑:我正在使用 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
}
有帮助吗?

解决方案

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

这基本上指出,当动画放在动画上时,OnAnimationEnd方法并不能很好地工作

解决方法是在您使用动画的视图中聆听动画事件,例如,如果您最初将动画侦听器附加到这样的动画中

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

然后将动画应用于这样的图像视图

mImageView.startAnimation(mAnimation);

要解决此问题,您现在必须创建一个自定义图像视图

public class MyImageView extends ImageView {

然后覆盖 onAnimationEnd 视图类的方法并在那里提供所有功能

@Override
protected void onAnimationEnd() {
    super.onAnimationEnd();
    //Functionality here
}

这是此问题的适当解决方法,在过度雷登视图中提供功能 - > naNimationEnd方法,而不是附加到动画上的动画列符的onAnimationEnd方法。

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

其他提示

我是通过打电话来解决这个问题的安倍 clearAnimation() 在视图上是动画的 OnAnimationEnd, ,这消除了闪烁,这很奇怪,为什么任何人都必须这样做,因为只有在动画已经结束的情况下,才应该打电话给NimimationEnd回调。但是我猜答案在于框架深度关于视图/布局处理动画回调的方式。现在将其作为无动用的解决方案,只是有效的。

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

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

它运行良好,但是最后,我发现了一个对我有用的简单解决方案。

打电话后 view.StartAnimation(animation), ,在我的程序下一步之前,我添加了一个短的延迟,足以让动画完成,但用户足够简短,用户不知所措:

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

我也有同样的问题,并使用

view.clearAnimation();

view.startAnimation(anim);

由于某种原因,OnAnimationStart可以正常工作,而OnAnimationEnd则无法。因此,我最初是如何做的以及我改变了什么:

尝试1(闪烁):a)将图像从0px移动到80px b)在OnAnimationEnd中,将图像的位置设置为80px

尝试2(无闪烁):a)在OnAnimationstart中,将图像的位置设置为80px b)将图像从-80px移动到0px

希望这是有道理的。基本上我翻转了我的方式

尝试从您的对象使用GetAnimation():

public void onShowListBtnClick(View view)
    {
        rightPanel.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));

        rightPanel.getAnimation().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) {
                // write your code here
            }
        });
}

屏幕旋转时也可以停止封口。在这种情况下,没有调用OnAnimationEnd()。我的解决方法:

 animation.setDuration(animationDuration);
 animation.setAnimationListener(new AnimationListenerAdapter() {...});
 view.startAnimation(animation);
 handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!animation.hasEnded()) {
                    // here you can handle this case
                }
            }
        }, animationDuration + 100);

一个简单的修复是将一行添加到 AnimationListener.onAnimationEnd():

@Override 
public void onAnimationEnd(Animation a) {
    a.setAnimationListener(null);
    …
}

我有这个问题,因为我的 Animation 没有在主线程中开始。这导致了 duration of0。但是, Animation 确实播放了 - 它只是叫 onAnimationEnd() 执行后立即。

如果您将重复计数用作无限,则不会被调用。检查文档 关联

这对我有用:

@Override
public void onAnimationUpdate(ValueAnimator animation) {
    if (Float.compare(animation.getAnimatedFraction(),1.0f)) {
    // animation ended;
        //do stuff post animation
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top