2012-11-22 109 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收集未使用的资源。