用AnimationDrawable加载逐帧动画OOM问题

之前帧动画10页,在大屏手机上,有的会出现oom,原来帧动画一次性加载10张图片,造成的oom.最好就修改成分布加载10张图片,不说了,直接上代码:

 

 

import android.widget.ImageView;

public class SceneAnimation {
 private ImageView mImageView;
 private int[] mFrameRess;
 private int[] mDurations;
 private int mDuration;

 private int mLastFrameNo;
 private long mBreakDelay;

 public SceneAnimation(ImageView pImageView, int[] pFrameRess,
   int[] pDurations) {
  mImageView = pImageView;
  mFrameRess = pFrameRess;
  mDurations = pDurations;
  mLastFrameNo = pFrameRess.length - 1;

  mImageView.setBackgroundResource(mFrameRess[0]);
  play(1);
 }

 public SceneAnimation(ImageView pImageView, int[] pFrameRess, int pDuration) {
  mImageView = pImageView;
  mFrameRess = pFrameRess;
  mDuration = pDuration;
  mLastFrameNo = pFrameRess.length - 1;

  mImageView.setBackgroundResource(mFrameRess[0]);
  playConstant(1);
 }

 public SceneAnimation(ImageView pImageView, int[] pFrameRess,
   int pDuration, long pBreakDelay) {
  mImageView = pImageView;
  mFrameRess = pFrameRess;
  mDuration = pDuration;
  mLastFrameNo = pFrameRess.length - 1;
  mBreakDelay = pBreakDelay;

  mImageView.setBackgroundResource(mFrameRess[0]);
  playConstant(1);
 }

 private void play(final int pFrameNo) {
  mImageView.postDelayed(new Runnable() {
   public void run() {
    mImageView.setBackgroundResource(mFrameRess[pFrameNo]);
    if (pFrameNo == mLastFrameNo)
     play(0);
    else
     play(pFrameNo + 1);
   }
  }, mDurations[pFrameNo]);
 }

 private void playConstant(final int pFrameNo) {
  mImageView.postDelayed(new Runnable() {
   public void run() {
    mImageView.setBackgroundResource(mFrameRess[pFrameNo]);

    if (pFrameNo == mLastFrameNo)
     playConstant(0);
    else
     playConstant(pFrameNo + 1);
   }
  }, pFrameNo == mLastFrameNo && mBreakDelay > 0 ? mBreakDelay
    : mDuration);
 }
};

   然后其他页面引用即可

你可能感兴趣的:(用AnimationDrawable加载逐帧动画OOM问题)