想做一个逐帧动画,用了一百来张图片,结果内存溢出了,找了半天最后算是解决了。
本来是在drawable里面写一个animation-list,设置ImageView的backgroud,
然后在Activity里面
- animationDrawable = (AnimationDrawable)mImageView.getBackground();
- animationDrawable.start();
最后采用的解决办法是这个http://www.kankanews.com/ICkengine/archives/86482.shtml
稍微改动了一下。
里面有三个构造方法,和两个播放的方法,直接构造就可以播放动画了
- package com.example.animationoom;
- import android.os.Handler;
- import android.widget.ImageView;
- public class SceneAnimation {
- private ImageView mImageView;
- private int[] mFrameRess;// 图片
- private int[] mDurations;
- private int mDuration;
- private Handler handler = new Handler();
- Runnable mRunnable;
- private int mLastFrameNo;
- private long mBreakDelay;
- //参数包括pDurations数组,执行播放play(1);
- public SceneAnimation(ImageView pImageView, int[] pFrameRess, int[] pDurations) {
- mImageView = pImageView;
- mFrameRess = pFrameRess;
- mDurations = pDurations;
- mLastFrameNo = pFrameRess.length - 1;
- mImageView.setBackgroundResource(mFrameRess[0]);
- play(1);
- }
- //参数包括一个int 的执行时间 pDuration, 执行循环播放playConstant(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);
- }
- //参数包括一个int的pDuration,一个long的pBreakDelay,表示每次播放的间隔,执行循环播放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) {
- mRunnable = new Runnable() {
- public void run() {
- mImageView.setBackgroundResource(mFrameRess[pFrameNo]);
- if (pFrameNo == mLastFrameNo)
- play(0);
- else
- play(pFrameNo + 1);
- }
- };
- handler.postDelayed( mRunnable, mDurations[pFrameNo]);
- }
- private void playConstant(final int pFrameNo) {
- Handler handler = new Handler();
- handler.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);
- }
- };