在Android中获取动画的变换矩阵

发布于 2024-08-27 02:24:43 字数 461 浏览 7 评论 0原文

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

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

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

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

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

如何使用getTransformation获取动画的变换矩阵?还有其他方法可以做到这一点吗?

I would like to obtain the transformation matrix of an animation to calculate the position of the view that was animated.

It seems the Animation class has a method called getTransformation that can be used to obtain the transformation applied to the view.

However, if I use getTransformation before starting the animation I obtain the identity matrix. And if I use it like this:

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

The program enters an infite loop because getTransformation seems to trigger onAnimationEnd (why?).

How should I use getTransformation to obtain the transformation matrix of an animation? Is there another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔小萌 2024-09-03 02:24:43

因为,出于某种原因,这就是 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);
}

Because, for some reason, that is the way getTransformation is coded.

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

It looks like you can check hasEnded() to see if the animation is complete. Try something like this:

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

   Transformation t= new Transformation();
   animation.getTransformation(animation.getDuration(), t);
}
×纯※雪 2024-09-03 02:24:43

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

我从 GrepCode,代码如下 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 = true 是在 onAnimationEnd() 调用之后设置的:

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

清除动画监听器。如果需要,我们可以在之后返回

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

I have met this problem too when I want to getTransformation() for next animation after previous animation done, then infinite loop cause stackoverflow.

I analyzed problem with source code of Android from GrepCode, with codes below in getTransformation(), I never find any problem which would cause infinite loop, as only place call onAnimationEnd(), it will set mEnded to true. Next time call getTransformation(), it should be blocked by if(!mEnded), but it's not.

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

It seams grepcode is not correct with this segments of code. The truth is that eEnded = true is set after onAnimationEnd() called:

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

That should explain why infinite loop happen. Even you check animation.hasEnded() would not help, as it could not reach to set mEnded to true.

The ways I hack with this problem are:
Set flag in listener to guard:

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

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

Clear AnimationListner. If required, we can set back after

public void onAnimationEnd(Animation animation)
{
   animation.setAnimationListener(null);
   Transformation t= new Transformation();
   animation.getTransformation(animation.getDuration(), t);
   animation.setAnimationListener(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文