如何使用 AnimationDrawable 暂停帧动画? [英] How do I pause frame animation using AnimationDrawable?

查看:43
本文介绍了如何使用 AnimationDrawable 暂停帧动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 AnimationDrawable 暂停帧动画?

How do I pause frame animation using AnimationDrawable?

推荐答案

我意识到这个帖子已经很老了,但是由于这是我在搜索暂停动画的方法时在 Google 上的第一个答案,所以我只会在这里发布解决方案供其他人查看.您需要做的是子类化您想要使用的动画类型,然后添加暂停和恢复动画的方法.以下是 AlphaAnimation 的示例:

I realize this thread is quite old, but since this was the first answer on Google when I was searching for a way to pause an animation, I'll just post the solution here for someone else to see. What you need to do is subclass the animation type you'd like to use and then add methods for pausing and resuming the animation. Here is an example for AlphaAnimation:

public class PausableAlphaAnimation extends AlphaAnimation {

    private long mElapsedAtPause=0;
    private boolean mPaused=false;

    public PausableAlphaAnimation(float fromAlpha, float toAlpha) {
        super(fromAlpha, toAlpha);
    }

    @Override
    public boolean getTransformation(long currentTime, Transformation outTransformation) { 
        if(mPaused && mElapsedAtPause==0) {
            mElapsedAtPause=currentTime-getStartTime();
        }
        if(mPaused)
            setStartTime(currentTime-mElapsedAtPause);
        return super.getTransformation(currentTime, outTransformation);
    }

    public void pause() {
        mElapsedAtPause=0;
        mPaused=true;
    }

    public void resume() {
        mPaused=false;
    }
}

这将在动画暂停时继续增加您的开始时间,有效地防止它完成并保持它在您暂停时的状态.

This will keep increasing your starttime while the animation is paused, effectively keeping it from finishing and keeping it's state where it was when you paused.

希望对某人有所帮助.

这篇关于如何使用 AnimationDrawable 暂停帧动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆