Android 帧动画何时停止播放
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
關(guān)于android幀動畫沒有提供結(jié)束播放的接口回調(diào)表示不理解,也許是基于播放動畫的時候,系統(tǒng)在干其他事情,無法確切地保證動畫在totalDuration時間內(nèi)播放完畢等。如果有哪位前輩知道原因,請不吝指教。????
盡管android沒有為幀動畫提供結(jié)束播放的接口,我們還是可以通過一些其他方式來做到。
方式一,當動畫開始start之后,我們可以通過檢測是否到達幀動畫的最后一幀,來確定動畫是否播完。這種方式可以保證動畫播完。
方式二,重寫AnimationDrawable,獲得totalDuration,然后動畫start之后的totalDuration,調(diào)用結(jié)束的接口回調(diào)onAnimationFinshed()。為什么必須重寫呢?因為,AnimationDrawable僅提供了每一幀的duration,而不能直接獲得動畫總的duration。
當然還有一些變種的方法,但是其大體思路都應(yīng)該差不多。至于動畫是否流暢播放,這要取決于你播放動畫的時候,系統(tǒng)的繁忙程度。
下面給出例子:
定義幀動畫:
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 {@Overridepublic 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 drawablefor (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 classCustomAnimationDrawableNew cad = new CustomAnimationDrawableNew((AnimationDrawable) getResources().getDrawable(R.drawable.anim_test)) {@Overridepublic void onAnimationFinish() {// Animation has finished...}};// Set the views drawable to our custom drawablev.setBackgroundDrawable(cad);// Start the animationcad.start();}}); 上述例子僅給出了其思路,如果你感興趣,可以自己動手實驗。轉(zhuǎn)載于:https://my.oschina.net/jerikc/blog/129703
總結(jié)
以上是生活随笔為你收集整理的Android 帧动画何时停止播放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 递归求乘积和
- 下一篇: 6.HBase时髦谨慎财会会计