AnimationDrawable 结束回调
来源:互联网 发布:淘宝访客量怎么刷的 编辑:程序博客网 时间:2024/05/16 19:16
关于android帧动画没有提供结束播放的接口回调表示不理解,也许是基于播放动画的时候,系统在干其他事情,无法确切地保证动画在totalDuration时间内播放完毕等。如果有哪位前辈知道原因,请不吝指教。
尽管android没有为帧动画提供结束播放的接口,我们还是可以通过一些其他方式来做到。
方式一,当动画开始start之后,我们可以通过检测是否到达帧动画的最后一帧,来确定动画是否播完。这种方式可以保证动画播完。
方式二,重写AnimationDrawable,获得totalDuration,然后动画start之后的totalDuration,调用结束的接口回调onAnimationFinshed()。为什么必须重写呢?因为,AnimationDrawable仅提供了每一帧的duration,而不能直接获得动画总的duration。
当然还有一些变种的方法,但是其大体思路都应该差不多。至于动画是否流畅播放,这要取决于你播放动画的时候,系统的繁忙程度。
下面给出例子:
定义帧动画:
res/anim/effect_animation.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
animation-list
android:oneshot
=
"true"
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_5"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_4"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_3"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_2"
/>
<
item
android:duration
=
"200"
android:drawable
=
"@drawable/curtain_0"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_2"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_3"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_4"
/>
<
item
android:duration
=
"0"
android:drawable
=
"@drawable/curtain_5"
/>
</
animation-list
>
思路一:
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
private
void
checkIfAnimationDone() {
mHandler.postDelayed(
new
Runnable() {
public
void
run() {
if
(mAnimationDrawable.getCurrent() != mAnimationDrawable
.getFrame(mAnimationDrawable.getNumberOfFrames() -
1
)) {
checkIfAnimationDone();
}
else
{
mHandler.sendEmptyMessage(MSG_ANIMATION_FINISHED);
}
}
}, mDuration);
};
private
void
playAnimation() {
if
(mView ==
null
) {
mView = mContext.getView();
}
mView.setBackgroundResource(R.anim.effect_animation);
mAnimationDrawable = (AnimationDrawable) mView.getBackground();
mAnimationDrawable.start();
for
(
int
i =
0
; i < mAnimationDrawable.getNumberOfFrames(); i++) {
mDuration += mAnimationDrawable.getDuration(i);
}
checkIfAnimationDone();
}
private
class
MainHandler
extends
Handler {
@Override
public
void
handleMessage(Message msg) {
if
(LOG) {
Log.v(TAG,
"msg id="
+ msg.what);
}
switch
(msg.what) {
case
MSG_ANIMATION_FINISHED:
if
(mView ==
null
) {
break
;
}
mView.setBackgroundResource(
0
);
break
;
default
:
break
;
}
}
}
思路二:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
The custom animation drawable
class
:
public
abstract
class
CustomAnimationDrawableNew
extends
AnimationDrawable {
// Handles the animation callback.
Handler mAnimationHandler;
public
CustomAnimationDrawableNew(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(
new
Runnable() {
public
void
run() {
onAnimationFinish();
}
}, 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.
*/
public
abstract
void
onAnimationFinish();
}
Use
this
class
:
ImageView iv = (ImageView) findViewById(R.id.iv_testing_testani);
iv.setOnClickListener(
new
OnClickListener() {
public
void
onClick(
final
View v) {
// Pass our animation drawable to our custom drawable class
CustomAnimationDrawableNew cad =
new
CustomAnimationDrawableNew(
(AnimationDrawable) getResources().getDrawable(
R.drawable.anim_test)) {
@Override
public
void
onAnimationFinish() {
// Animation has finished...
}
};
// Set the views drawable to our custom drawable
v.setBackgroundDrawable(cad);
// Start the animation
cad.start();
}
});
0 0
- AnimationDrawable 结束回调
- AnimationDrawable动画最后一贞结束侦听。
- AnimationDrawable
- AnimationDrawable
- AnimationDrawable
- AnimationDrawable
- AnimationDrawable
- AnimationDrawable监听播放结束及ImageSwitcher动画图片切换,带动画
- AlphaAnimation 、AnimationDrawable
- AnimationDrawable OOM
- Cocos2dX 动作结束回调应用
- 获取视图树,对结束事件进行监听,layout结束回调onGlobalLayout方法
- 逐帧动画 AnimationDrawable
- Android 之AnimationDrawable
- android之AnimationDrawable
- android之AnimationDrawable
- android AnimationDrawable的使用
- AnimationDrawable动画实现
- 动态规划法——求解0-1背包问题
- 几个典型的蓝屏故障
- Swft UInt->Int 或者 Int->UInt
- ExtJs4修改默认字体
- Solr 报错:RemoteSolrException: Expected mime type application/octet-stream but got text/html
- AnimationDrawable 结束回调
- C语言动态规划(3)___Filthy Rich(Hdu 2391)
- zjut_1185 十进制转换为二进制
- 虚拟机kali安装vmtools
- 背包问题再理解
- Centos搭建SVN服务器三步曲
- 不能为虚拟电脑 xxx 打开一个新任务. Failed to load VMMR0.r0 (VERR_SUPLIB_OWNER_NOT_ROOT). 返回 代码:NS_ERROR_FAIL
- Triangle
- RevitAPI:如何用API实现两条管道按中心线(Center Line)对齐