捕获Android帧动画的结束事件

By | 2011/08/13

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

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

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

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

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

/**
* 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

发表评论

电子邮件地址不会被公开。 必填项已用*标注