2

我想获得动画的变换矩阵来计算动画视图的位置。

Animation 类似乎有一个名为 getTransformation 的方法,可用于获取应用于视图的转换。

但是,如果我在开始动画之前使用 getTransformation ,我会获得单位矩阵。如果我这样使用它:

public void onAnimationEnd(Animation animation) {
    Transformation t = new Transformation();
    animation.getTransformation(animation.getDuration(), t);
}

程序进入一个无限循环,因为 getTransformation 似乎触发了 onAnimationEnd(为什么?)。

我应该如何使用 getTransformation 来获取动画的变换矩阵?还有另一种方法可以做到这一点吗?

4

2 回答 2

0

当我想在上一个动画完成后为下一个动画获取转换()时,我也遇到了这个问题,然后无限循环导致堆栈溢出。

我从GrepCode分析了 Android 源代码的问题,下面的代码getTransformation(),我从来没有发现任何会导致无限循环的问题,因为只有地方调用 onAnimationEnd(),它会设置mEnded为 true。下次调用getTransformation(),应该会被 阻止if(!mEnded),但事实并非如此。

    if (expired) {
        if (mRepeatCount == mRepeated) {
            if (!mEnded) {
                mEnded = true;   //this is not real!!!
                if (mListener != null) {
                    mListener.onAnimationEnd(this);
                }
            }
        } else {
            if (mRepeatCount > 0) {
                mRepeated++;
            }

            if (mRepeatMode == REVERSE) {
                mCycleFlip = !mCycleFlip;
            }

            mStartTime = -1;
            mMore = true;

            if (mListener != null) {
                mListener.onAnimationRepeat(this);
            }
        }
    }

它接缝 grepcode 与这段代码不正确。事实是,eEnded = trueonAnimationEnd()调用后设置:

    if (!mEnded) {
            if (mListener != null) {
                mListener.onAnimationEnd(this);
            }
            //it happen after onAnimationEnd(). cancel() is also does as this
            mEnded = true; 
        }

这应该可以解释为什么会发生无限循环。即使您检查animation.hasEnded()也无济于事,因为它无法将 mEnded 设置为 true。

我解决这个问题的方法是:
在侦听器中设置标志以保护:

boolean needBlock = false;
public void onAnimationEnd(Animation animation)
{
   if ( needBlock ) return;

   needBlock = true;
   Transformation t= new Transformation();
   animation.getTransformation(animation.getDuration(), t);
}

清除 AnimationListner。如果需要,我们可以在之后设置

public void onAnimationEnd(Animation animation)
{
   animation.setAnimationListener(null);
   Transformation t= new Transformation();
   animation.getTransformation(animation.getDuration(), t);
   animation.setAnimationListener(this);
}
于 2013-03-06T19:49:40.317 回答
0

因为,出于某种原因,这就是 getTransformation 的编码方式。

http://www.netmite.com/android/mydroid/1.5/frameworks/base/core/java/android/view/animation/Animation.java

看起来您可以检查 hasEnded() 以查看动画是否完整。尝试这样的事情:

public void onAnimationEnd(Animation animation)
{
   if ( animation.hasEnded() )
       return;

   Transformation t= new Transformation();
   animation.getTransformation(animation.getDuration(), t);
}
于 2010-08-17T16:55:55.603 回答