Android DrawableAnimation逐帧动画加载多图(OOM的解决)

想做一个逐帧动画,用了一百来张图片,结果内存溢出了,找了半天最后算是解决了。

本来是在drawable里面写一个animation-list,设置ImageView的backgroud,

然后在Activity里面

[java]  view plain  copy
  1. animationDrawable = (AnimationDrawable)mImageView.getBackground();  
[java]  view plain  copy
  1. animationDrawable.start();  
但是图片多了就会内存溢出了。


最后采用的解决办法是这个http://www.kankanews.com/ICkengine/archives/86482.shtml

稍微改动了一下。

里面有三个构造方法,和两个播放的方法,直接构造就可以播放动画了

[java]  view plain  copy
  1. package com.example.animationoom;  
  2.   
  3. import android.os.Handler;  
  4. import android.widget.ImageView;  
  5.   
  6. public class SceneAnimation  {  
  7.     private ImageView mImageView;  
  8.     private int[] mFrameRess;// 图片  
  9.     private int[] mDurations;  
  10.     private int mDuration;  
  11.       
  12.     private Handler handler = new Handler();  
  13.     Runnable mRunnable;  
  14.   
  15.     private int mLastFrameNo;  
  16.     private long mBreakDelay;  
  17.       
  18.     //参数包括pDurations数组,执行播放play(1);  
  19.     public SceneAnimation(ImageView pImageView, int[] pFrameRess, int[] pDurations) {  
  20.         mImageView = pImageView;  
  21.         mFrameRess = pFrameRess;  
  22.         mDurations = pDurations;  
  23.         mLastFrameNo = pFrameRess.length - 1;  
  24.   
  25.         mImageView.setBackgroundResource(mFrameRess[0]);  
  26.         play(1);  
  27.     }  
  28.       
  29.     //参数包括一个int 的执行时间 pDuration, 执行循环播放playConstant(1);  
  30.     public SceneAnimation(ImageView pImageView, int[] pFrameRess, int pDuration) {  
  31.         mImageView = pImageView;  
  32.         mFrameRess = pFrameRess;  
  33.         mDuration = pDuration;  
  34.         mLastFrameNo = pFrameRess.length - 1;  
  35.   
  36.         mImageView.setBackgroundResource(mFrameRess[0]);  
  37.         playConstant(1);  
  38.     }  
  39.       
  40.     //参数包括一个int的pDuration,一个long的pBreakDelay,表示每次播放的间隔,执行循环播放playConstant(1);  
  41.     public SceneAnimation(ImageView pImageView, int[] pFrameRess,int pDuration, long pBreakDelay) {  
  42.         mImageView = pImageView;  
  43.         mFrameRess = pFrameRess;  
  44.         mDuration = pDuration;  
  45.         mLastFrameNo = pFrameRess.length - 1;  
  46.         mBreakDelay = pBreakDelay;  
  47.   
  48.         mImageView.setBackgroundResource(mFrameRess[0]);  
  49.         playConstant(1);  
  50.     }  
  51.   
  52.     private void play(final int pFrameNo) {  
  53.           
  54.         mRunnable = new Runnable() {  
  55.             public void run() {  
  56.                 mImageView.setBackgroundResource(mFrameRess[pFrameNo]);  
  57.                 if (pFrameNo == mLastFrameNo)  
  58.                     play(0);  
  59.                 else  
  60.                     play(pFrameNo + 1);  
  61.             }  
  62.         };  
  63.         handler.postDelayed( mRunnable, mDurations[pFrameNo]);  
  64.     }  
  65.   
  66.     private void playConstant(final int pFrameNo) {  
  67.         Handler handler = new Handler();  
  68.         handler.postDelayed(new Runnable() {  
  69.             public void run() {  
  70.                 mImageView.setBackgroundResource(mFrameRess[pFrameNo]);  
  71.   
  72.                 if (pFrameNo == mLastFrameNo)  
  73.                     playConstant(0);  
  74.                 else  
  75.                     playConstant(pFrameNo + 1);  
  76.             }  
  77.         }, pFrameNo == mLastFrameNo && mBreakDelay > 0 ? mBreakDelay : mDuration);  
  78.     }  
  79.       
  80.   
  81. };  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值