天天看點

android 動畫釋放,從AnimationDrawable釋放記憶體

我在我的應用程式的許多活動中使用了animationdrawables。這是代碼inicializate他們:從AnimationDrawable釋放記憶體

public void prepareVideo (int resourceBall, String animation, int width, int height){

imgBall = (ImageView)findViewById(resourceBall);

imgBall.setVisibility(ImageView.VISIBLE);

String resourceNameAnimation = animation;

int id_res = getResources().getIdentifier(resourceNameAnimation, "drawable", getPackageName());

imgBall.setBackgroundResource(id_res);

LayoutParams latoutFrame = imgBall.getLayoutParams();

latoutFrame.width = width;

latoutFrame.height = height;

imgBall.setLayoutParams(latoutFrame);

frame = (AnimationDrawable) imgBall.getBackground();

}

然後我打電話: imgBall.post(新StartAnimation());

調用可運作:

class StartAnimation implements Runnable {

public void run() {

frame.start();

}

}

問題是我使用許多動畫,是使用相同的XML(逐幀)。我必須在許多具有相同動畫的活動中調用此方法。最後,我得到了一個超出記憶體的錯誤。我試圖以釋放記憶體螢幕之間:

for (int i = 0; i < frame.getNumberOfFrames(); ++i){

Drawable frame_rescue = frame.getFrame(i);

if (frame_rescue instanceof BitmapDrawable) {

((BitmapDrawable)frame_rescue).getBitmap().recycle();

}

frame_rescue.setCallback(null);

的問題是,當我再次嘗試使用resourdeŸ得到其他錯誤: “試圖使用回收的位圖”

任何人都知道其他的方式空閑的記憶體?

2012-11-22

Joterito