捕获Android帧动画的结束事件

By | 2011/08/13

最近在一个Android应用中,用到了帧动画。这东西的具体介绍就不讲了,网上到处是(虽然基本都是抄来抄去……)。用起来很简单效果也很好,不过这一次我有一个特殊的要求,希望帧动画在播放完毕的时候做一些其他的事情。

在渐变动画中,我们可以很简单的使用监听器:

XXX.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        //渐变动画结束....
    }
});

然而帧动画却没有这样方便的功能,为什么??

当然,我们自己写的动画,自己应该是知道它会什么时候结束,启动动画的时候顺便启动一个线程就好了,说说简单做起来毕竟也是要花时间的啊,咱自己写一个类吧,以后就好重复用了,共享出来,要是谁也有这样的要求就直接用吧!

public abstract class MyAnimationDrawable extends AnimationDrawable {
    Handler finishHandler;      // 判断结束的Handler
    public MyAnimationDrawable(AnimationDrawable ad) {
        // 这里得自己把每一帧加进去
        for (int i = 0; i < ad.getNumberOfFrames(); i++) {
            this.addFrame(ad.getFrame(i), ad.getDuration(i));
        }
    }
    @Override
    public void start() {
        super.start();
        /**
         * 首先用父类的start()
         * 然后启动线程,来调用onAnimationEnd()
         */
        finishHandler = new Handler();
        finishHandler.postDelayed(
            new Runnable() {
                public void run() {
                    onAnimationEnd();
                }
            }, getTotalDuration());
    }
    /**
     * 这个方法获得动画的持续时间(之后调用onAnimationEnd())
     */
    public int getTotalDuration() {
        int durationTime = 0;
        for (int i = 0; i < this.getNumberOfFrames(); i++) {
            durationTime += this.getDuration(i);
        }
        return durationTime;
    }
    /**
     * 结束时调用的方法,一定要实现
     */
    abstract void onAnimationEnd();
}

/******************************************
 ***************** 使用方法 *****************
 ******************************************/

// 新建我们的类的实例
MyAnimationDrawable mad = new MyAnimationDrawable(
    (AnimationDrawable) getResources().getDrawable(R.drawable.anim1)) {
        @Override
        void onAnimationEnd() {
            // 实现这个方法,结束后会调用
        }
    };
// 把这个动画“赐福”给某个ImageView
iv.setBackgroundDrawable(mad);
// 开始吧
mad.start();

但是这个方法未必好,为什么呢,因为我们没法保证动画会完整流畅的播放完毕,也许因为其他的事情终止了,也不知到机器卡的时候会不会使动画播放时间变长,仅用在要求不高的情况下,至少,我这里是够了。要是有更好方法一定告知~

/**
* This class is a hack to simply get an on finish callback for an
* AnimationDrawable.
*
*/
public class CustomAnimationDrawable extends AnimationDrawable {
// We need to keep our own frame count because mCurFrame in AnimationDrawable is private
private int mCurFrameHack = -1;

// This is the Runnable that we will call when the animation finishes
private Runnable mCallback = null;

public CustomAnimationDrawable(AnimationDrawable aniDrawable) {
// TODO Auto-generated constructor stub
for(int i=0;i {
this.addFrame(aniDrawable.getFrame(i), aniDrawable.getDuration(i));
}
}

/*
* We override the run method in order to increment the frame count the same
* way the AnimationDrawable class does. Also, when we are on the last frame we
* schedule our callback to be called after the duration of the last frame.
*/
@Override
public void run() {
super.run();

mCurFrameHack += 1;
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) {
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack));
stop();
}
}

/*
* We override this method simply to reset the frame count just as is done in
* AnimationDrawable.
*/
@Override
public void unscheduleSelf(Runnable what) {
// TODO Auto-generated method stub
super.unscheduleSelf(what);
mCurFrameHack = -1;
}

public void setOnFinishCallback(Runnable callback) {
mCallback = callback;
}

public Runnable getOnFinishCallback() {
return mCallback;
}

}

and I use this class like this,

private void registerAnimation(int id, final Runnable cb)
{
final ImageView imgView = (ImageView)findViewById(id);
final CustomAnimationDrawable aniDrawable = new CustomAnimationDrawable((AnimationDrawable)imgView.getBackground());
imgView.setBackgroundDrawable(aniDrawable.getFrame(0));

aniDrawable.setOnFinishCallback(cb);

imgView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!aniDrawable.isRunning())
{
v.setBackgroundDrawable(aniDrawable);
aniDrawable.start();
}
}
});
}

One thought on “捕获Android帧动画的结束事件

  1. Dufresne

    要想准确判断的话,还可以使用反射机制去挖掘一些东东来判断。

    Reply

发表评论

您的电子邮箱地址不会被公开。