2012-11-22 107 views
5

我在我的應用程序的許多活動中使用了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Ÿ得到其他錯誤: 「試圖使用回收的位圖」

任何人都知道其他的方式空閒的內存?

回答

0

我有同樣的問題,問題是可運行的。 爲每個動畫製作一個獨立線程使animationDrawables在銷燬活動並清除視圖和回調之後保持不變。 因此,在沒有獨立線程的情況下啓動動畫可讓GC收集未使用的資源。