android动画未在onAnimationEnd中完成

android动画未在onAnimationEnd中完成

本文介绍了android动画未在onAnimationEnd中完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It seems that an android animation is not truly finished when the onAnimationEnd event is fired although animation.hasEnded is set to true.

我希望我的视图在它的 ScaleAnimation 结束时更改它的背景可绘制,但您可以清楚地看到它在完成前几毫秒发生了变化.问题是,它闪烁是因为新背景出现(=被)缩放了很短的时间,直到动画真正完成.

I want my view to change it's background drawable on the end of it's ScaleAnimation which it does, but you can clearly see that it is changed some miliseconds before it finishes. The problem is, that it flickers because the new background appears (=is) scaled for a short time until the animation really finishes.

有没有办法让动画真正结束,或者只是防止新背景在这么短的时间内被缩放?

Is there a way to get either the real end of the animation or just prevent the new background from beeing scaled this short period of time?

谢谢!

//我正在使用 AnimationListener 来获得以下调用:

// I'm using an AnimationListener to get the following call:

    @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

Here is the actual bug related to this issue http://code.google.com/p/android-misc-widgets/issues/detail?id=8

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

This basically states that the onAnimationEnd method doesn't really work well when an AnimationListener is attached to an Animation

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

The workaround is to listen for the animation events in the view to which you were applying the animation toFor example if initially you were attaching the animation listener to the animation like this

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

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

and then applying to the animation to a ImageView like this

mImageView.startAnimation(mAnimation);

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

To work around this issue, you must now create a custom ImageView

public class MyImageView extends ImageView {

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

and then override the onAnimationEnd method of the View class and provide all the functionality there

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

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

This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd method as opposed to the onAnimationEnd method of the AnimationListener attached to the Animation.

这工作正常,动画结束时不再有任何闪烁.希望这会有所帮助.

This works properly and there is no longer any flicker towards the end of the animation. Hope this helps.

这篇关于android动画未在onAnimationEnd中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:04