关于android帧动画没有提供结束播放的接口回调表示不理解,也许是基于播放动画的时候,系统在干其他事情,无法确切地保证动画在totalDuration时间内播放完毕等。如果有哪位前辈知道原因,请不吝指教。

尽管android没有为帧动画提供结束播放的接口,我们还是可以通过一些其他方式来做到。

方式一,当动画开始start之后,我们可以通过检测是否到达帧动画的最后一帧,来确定动画是否播完。这种方式可以保证动画播完。

方式二,重写AnimationDrawable,获得totalDuration,然后动画start之后的totalDuration,调用结束的接口回调onAnimationFinshed()。为什么必须重写呢?因为,AnimationDrawable仅提供了每一帧的duration,而不能直接获得动画总的duration。

当然还有一些变种的方法,但是其大体思路都应该差不多。至于动画是否流畅播放,这要取决于你播放动画的时候,系统的繁忙程度。

下面给出例子:

定义帧动画:

res/anim/effect_animation.xml

<?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>

思路一:

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

思路二:

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));    }}@Overridepublic 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();        }    });
上述例子仅给出了其思路,如果你感兴趣,可以自己动手实验。

更多相关文章

  1. Android逐帧动画——让图片动起来
  2. Android 动画分析之Tween动画分析
  3. Freescale IMX6 Android (7): Android启动动画死循环 Home界面不
  4. android 解决帧动画卡顿情况
  5. Android实现过渡动画、引导页 Android判断是否第一次启动App
  6. android 降低Crash率的方案分析和解决思路,干货分享

随机推荐

  1. 第一章 工欲善其事 必先利其器—Android
  2. Android Studio 4.0 New Class 新特性解
  3. android 事件机制图文详解-从源码角度分析
  4. power_supply子系统笔记
  5. 【Android Training - 05】与其他Apps进
  6. Eclipse与Android源码中ProGuard工具的使
  7. 手机相册的基本使用
  8. Android 软键盘的显示和隐藏
  9. OOM的出现及解决(加载图片)
  10. 零基础开发OpenGL ES 2.0学习笔记-Android