【问题标题】:Free memory from AnimationDrawable从 AnimationDrawable 释放内存
【发布时间】:2012-11-22 15:35:01
【问题描述】:

我在我的应用程序的许多活动中使用动画绘图。这是初始化它们的代码:

 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(new StartAnimation());

调用runnable:

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);

问题是,当我再次尝试使用资源时,会出现其他错误: "尝试使用回收位图"

有人知道其他释放内存的方法吗?

【问题讨论】:

    标签: android animationdrawable


    【解决方案1】:

    我有同样的问题,问题是可运行的。 为每个动画制作一个独立的线程使得animationDrawables 在销毁活动并清除视图和回调后仍然存在。 所以在没有独立线程的情况下启动动画可以让 GC 收集未使用的资源。

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 1970-01-01
      • 2021-09-17
      • 2015-10-19
      • 1970-01-01
      • 2011-05-10
      • 2014-02-14
      • 2018-11-08
      • 1970-01-01
      相关资源
      最近更新 更多