AnimationDrawable暂停?

use*_*079 1 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);


    }
Run Code Online (Sandbox Code Playgroud)

}

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

小智 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();
}
Run Code Online (Sandbox Code Playgroud)