Android 解决帧动画卡顿问题

Android 解决帧动画卡顿问题

Android帧动画一次性加载会造成ui卡顿,所以就有了这份代码。通过handle队列和Bitmap复用,每次加载一张并显示,可以解决帧动画卡顿问题。

使用方法:

int[] right_res_id = new int[]{R.drawable.r_00072, R.drawable.r_00073, R.drawable.r_00074, R.drawable.r_00075};

final AnimationsContainer.FramesSequenceAnimation rightAnim =
                
      AnimationsContainer.getInstance().createProgressDialogAnim(imgRight,right_res_id);

rightAnim.setOnAnimStopListener(new AnimationsContainer.OnAnimationListener() {
            @Override
            public void onAnimationStart(AnimationsContainer.FramesSequenceAnimation animation) {
                //TODO
            }

            @Override
            public void onAnimationEnd(AnimationsContainer.FramesSequenceAnimation animation) {
                //TODO

            }

            @Override
            public void onAnimationStopOrCancel(AnimationsContainer.FramesSequenceAnimation animation) {
                //TODO
            }
        });
//rightAnim.setLoop(true);
//rightAnim.setGoBack(true); //只有动画停止时才会调用
//rightAnim.setDelayMillis(58); //设置每帧播放时间,与setDuration(long)二选一
rightAnim.setDuration(3000); //设置播放总时间
rightAnim.start();

//rightAnim.stop();
//rightAnim.goBackStart(); //返回第一帧

//rightAnim.cancel();

代码AnimationsContainer.java

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Handler;
import android.widget.ImageView;

import java.lang.ref.Soft