AnimationDrawable暂停?

时间:2012-05-25 13:39:10

标签: android

您好我试图暂停动画Drawable但无法成功。在堆栈溢出的帮助下,我至少得到了animationDrawable结束监听器的帮助,这里是它的代码。有没有可能的方法我可以暂停动画Drawable并从它暂停的地方开始...

public abstract class CustomAnimationDrawable extends AnimationDrawable{

    /** Handles the animation callback. */
    Handler mAnimationHandler;
    Runnable r= new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
               onAnimationFinish();
        }
    };

    public CustomAnimationDrawable(AnimationDrawable aniDrawable) {
        /* Add each frame to our animation drawable */
        for (int i = 0; i < aniDrawable.getNumberOfFrames(); i++) {
            this.addFrame(aniDrawable.getFrame(i), aniDrawable.getDuration(i));
        }
    }

    @Override
    public void start() {
        super.start();
        /*
         * Call super.start() to call the base class start animation method.
         * Then add a handler to call onAnimationFinish() when the total
         * duration for the animation has passed
         */
        mAnimationHandler = new Handler();
        mAnimationHandler.postDelayed(r, getTotalDuration());

    }

    /**
     * Gets the total duration of all frames.
     * 
     * @return The total duration.
     */
    public int getTotalDuration() {

        int iDuration = 0;

        for (int i = 0; i < this.getNumberOfFrames(); i++) {
            iDuration += this.getDuration(i);
        }

        return iDuration;
    }

    /**
     * Called when the animation finishes.
     */
    abstract void onAnimationFinish();

     public void destroy()
    {


         mAnimationHandler.removeCallbacksAndMessages(r);
         mAnimationHandler.removeCallbacksAndMessages(null);


    }

}

请帮助我有什么方法可以暂停吗?

3 个答案:

答案 0 :(得分:7)

我一直在四处寻找,似乎没有办法直接做到这一点。在AnimationDrawable中设置框架的所有变量和方法都是私有的。但是,当您点击暂停时,您可以获取动画所在帧的索引,然后通过从最后一帧播放的索引中迭代原始动画来创建新动画(然后重新开始并在动画中添加其余动画)是周期性的。

我开始使用基于XML的动画循环,然后添加另一个用于暂停和恢复。这是我最终做的,到目前为止工作正常,首先列出相关变量。

private ImageView sphere;
private AnimationDrawable sphereAnimation;
private AnimationDrawable sphereResume;
private AnimationDrawable activeAnimation;
private Drawable currentFrame;
private Drawable checkFrame;
private int frameIndex;

private void pause()
{
    looping = false;
    sphereResume = new AnimationDrawable();
    activeAnimation.stop();
    currentFrame = activeAnimation.getCurrent();

    frameLoop:
    for(int i = 0; i < sphereAnimation.getNumberOfFrames(); i++)
    {
        checkFrame = activeAnimation.getFrame(i);

        if(checkFrame == currentFrame)
        {
            frameIndex = i;
            for(int k = frameIndex; k < activeAnimation.getNumberOfFrames(); k++)
            {
                Drawable frame = activeAnimation.getFrame(k);
                sphereResume.addFrame(frame, 50);
            }
            for(int k = 0; k < frameIndex; k++)
            {
                Drawable frame = activeAnimation.getFrame(k);
                sphereResume.addFrame(frame, 50);
            }
            activeAnimation = sphereResume;
            sphere.setImageDrawable(activeAnimation);
            sphere.invalidate();
            break frameLoop;
        }
    }
}

private void play()
{
    looping = false;
    activeAnimation.setOneShot(true);
    activeAnimation.start();
}

private void stop()
{
    looping = false;
    activeAnimation.stop();
    activeAnimation = sphereAnimation;
    sphere.setImageDrawable(activeAnimation);
}

private void loop()
{
    looping = true;
    stopSoundEffect();
    activeAnimation.setOneShot(false);
    activeAnimation.start();
}

答案 1 :(得分:0)

Zorbert Crenshaw的方法很好,但它很复杂。

我有另一个解决方案。(需要使用try / catch环绕,因为反射是不安全的。)

您可以在停止动画之前调用 getCurFrameFromAnimationDrawable 来保存当前帧,并调用 setCurFrameToAnimationDrawable 从任何框架。

'30L'应该替换你的持续时间。

private int getCurFrameFromAnimationDrawable(AnimationDrawable drawable){
    try {
        Field mCurFrame = drawable.getClass().getDeclaredField("mCurFrame");
        mCurFrame.setAccessible(true);
        return  mCurFrame.getInt(drawable);
    }catch (Exception ex){
        Log.e(AnimTool.class.getName(), "getCurFrameFromAnimationDrawable: Exception");
        return 0;
    }
}

private void setCurFrameToAnimationDrawable(AnimationDrawable drawable, int curFrame){
    try {
        Field mCurFrame = drawable.getClass().getDeclaredField("mCurFrame");
        mCurFrame.setAccessible(true);
        mCurFrame.setInt(drawable, curFrame);
        Class[] param = new Class[]{Runnable.class, long.class};
        Method method = Drawable.class.getDeclaredMethod("scheduleSelf", param);
        method.setAccessible(true);
        method.invoke(drawable, drawable, 30L);
    }catch (Exception ex){
        Log.e(AnimTool.class.getName(), "setCurFrameToAnimationDrawable: Exception");
    }
}

答案 2 :(得分:0)

这是该问题的较晚答案。但这可能对另一个有帮助。要暂停和恢复AnimationDrawable,只需对其进行自定义并仅使用一个变量即可处理。

class CustomAnimationDrawable1() : AnimationDrawable() {

private var finished = false
private var animationFinishListener: AnimationFinishListener? = null
private var isPause = false
private var currentFrame = 0

fun setFinishListener(animationFinishListener: AnimationFinishListener?) {
    this.animationFinishListener = animationFinishListener
}

interface AnimationFinishListener {
    fun onAnimationFinished()
}


override fun selectDrawable(index: Int): Boolean {
    val ret = if (isPause) {
        super.selectDrawable(currentFrame)
    } else {
        super.selectDrawable(index)
    }

    if (!isPause) {
        currentFrame = index
        if (index == numberOfFrames - 1) {
            if (!finished && ret && !isOneShot) {
                finished = true
                if (animationFinishListener != null) animationFinishListener!!.onAnimationFinished()
            }
        }
    }
    return ret
}

fun pause() {
    isPause = true
}

fun resume() {
    isPause = false
}

}