AnimationDrawable暂停? [英] AnimationDrawable Pause ?

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

问题描述

您好,我试图暂停动画绘制对象,但无法成功。随着堆栈溢出的帮助下,我得到了ATLEAST结束animationDrawable听者的帮助这里是code吧。是否有任何可能的方式,我可以暂停动画绘制对象并启动它从那里已暂停...

Hello I was trying to Pause Animation Drawable but could not succeeded. With the help of stack overflow I atleast got the help of animationDrawable end listener here is the code for it . IS there any possible way where I can pause the animation Drawable and Start it from where it has paused ...

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


    }

}

请请大家帮忙有什么办法,我可以暂停?

Kindly please help is there any way i can pause ?

推荐答案

我一直在四处寻找,它似乎并不认为有一个办法直接做到这一点。所有的变量和设置在AnimationDrawable框架方法是私有的。但是,就可以得到该帧的动画索引当你打的停顿,然后通过从最后一帧的指数原创动画迭代创建一个新的动画发挥(然后再重新开始,如果动画添加其余是周期性的)。

I've been looking around, and it doesn't seem that there's a way to directly do it. All of the variables and methods for setting the frame in AnimationDrawable are private. However, you can get the index of the frame the animation is on when you hit pause and then create a new animation by iterating through the original animation from the index of the last frame played (and then start over and add the rest if the animation is cyclical).

我开始了一种基于XML的动画循环,然后只是增加了一个又一个的暂停和恢复。下面是我落得这样做,它的工作的罚款,到目前为止,相关的变量列在最前面。

I started out with an XML based animation loop, and then just added another one for pause and resume. Here's what I ended up doing, and it's working fine so far, with relevant variables listed first.

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

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

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