计算帧间动画的时间方法

代码来源:开源库DragIndicatorView

代码如下:


  protected void killView(final float x, final float y) {
        final ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(R.drawable.clean_anim);
        mRootView.addView(imageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.post(new Runnable() {
            @Override
            public void run() {
                imageView.setX(x - (imageView.getMeasuredWidth() >> 1));
                imageView.setY(y - (imageView.getMeasuredHeight() >> 1));
            }
        });

        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
        int totalDuring = 0;
        for (int i = 0, len = animationDrawable.getNumberOfFrames(); i < len; i++) {
            totalDuring += animationDrawable.getDuration(i);
        }
        animationDrawable.start();

        //动画播放结束后 移除ImageView
        postDelayed(new Runnable() {
            @Override
            public void run() {
                mRootView.removeView(imageView);
            }
        }, totalDuring + 20);

        if (mOnDismissAction != null) {
            mOnDismissAction.OnDismiss(this);
        }

        setVisibility(View.GONE);
    }

其中帧间动画为clean_anim.xml


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@drawable/clean_1"
        android:duration="120" />
    <item
        android:drawable="@drawable/clean_2"
        android:duration="120" />
    <item
        android:drawable="@drawable/clean_3"
        android:duration="120" />
    <item
        android:drawable="@drawable/clean_4"
        android:duration="120" />
    <item
        android:drawable="@drawable/clean_5"
        android:duration="120" />
    <item
        android:drawable="@android:color/transparent"
        android:duration="120" />
</animation-list>




;