最近在一个Android应用中,用到了帧动画。这东西的具体介绍就不讲了,网上到处是(虽然基本都是抄来抄去……)。用起来很简单效果也很好,不过这一次我有一个特殊的要求,希望帧动画在播放完毕的时候做一些其他的事情。
在渐变动画中,我们可以很简单的使用监听器:
1
2
3
4
5
6
7
8
9
10
11
12
|
XXX.setAnimationListener(
new
AnimationListener() {
@Override
public
void
onAnimationStart(Animation animation) {
}
@Override
public
void
onAnimationRepeat(Animation animation) {
}
@Override
public
void
onAnimationEnd(Animation animation) {
//渐变动画结束....
}
});
|
然而帧动画却没有这样方便的功能,为什么??
当然,我们自己写的动画,自己应该是知道它会什么时候结束,启动动画的时候顺便启动一个线程就好了,说说简单做起来毕竟也是要花时间的啊,咱自己写一个类吧,以后就好重复用了,共享出来,要是谁也有这样的要求就直接用吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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();
finishHandler =
new
Handler();
finishHandler.postDelayed(
new
Runnable() {
public
void
run() {
onAnimationEnd();
}
}, getTotalDuration());
}
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();
|
但是这个方法未必好,为什么呢,因为我们没法保证动画会完整流畅的播放完毕,也许因为其他的事情终止了,也不知到机器卡的时候会不会使动画播放时间变长,仅用在要求不高的情况下,至少,我这里是够了。要是有更好方法一定告知~
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));
}
}
@Override
public void run() {
super.run();
mCurFrameHack += 1;
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) {
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack));
stop();
}
}
@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();
}
}
});