2012-03-06 38 views
4

我已经准备好节数可绘制animations.when应用推出的第一个动画将start.i有两个按钮(下一个和以前)有相同的activity.when我点击下一步按钮我得到异常喜欢,如何解决android中的OutOfMemoryError?

java.lang.OutOfMemoryError: bitmap size exceeds VM budget 

我的代码,

为了得到从绘制绘制动画,

private void getAnimationForLetters(int mAnim) { 
     // TODO Auto-generated method stub 
     String anim = "capital_anim_" + new Integer(mAnim).toString(); 

     final int resID = getApplicationContext().getResources().getIdentifier(
       anim, "drawable", "my-package-name"); 
     mImgLetter.clearAnimation(); 
     mImgLetter.setBackgroundResource(resID); 
     mImgLetter.post(new Runnable() { 

      public void run() { 
       loadingAnimation = (AnimationDrawable) mImgLetter 
         .getBackground(); 
       loadingAnimation.start(); 
      } 
     }); 

    } 

我的下一个按钮的代码是,

case R.id.btnNext_: 
unbindDrawables(findViewById(R.id.imgLetter)); 
mLetterNum=mLetterNum+1; 
getAnimationForLetters(mLetterNum); 

我undind绘制方法的代码,

private void unbindDrawables(View view) { 
     if (view.getBackground() != null) { 
      view.getBackground().setCallback(null); 
     } 
     if (view instanceof ViewGroup) { 
      for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
       unbindDrawables(((ViewGroup) view).getChildAt(i)); 
      } 
      ((ViewGroup) view).removeAllViews(); 
     } 
    } 

终于,我得到异常java.lang.OutOfMemoryError: bitmap size exceeds VM budget 它显示例外这里mImgLetter.setBackgroundResource(resID); 请帮助我。

我加入如下代码清除,

loadingAnimation.stop(); 
     for (int i = 0; i < loadingAnimation.getNumberOfFrames(); ++i){ 
      Drawable frame = loadingAnimation.getFrame(i); 
      if (frame instanceof BitmapDrawable) { 
       ((BitmapDrawable)frame).getBitmap().recycle(); 
      } 
      frame.setCallback(null); 
     } 
     loadingAnimation.setCallback(null); 

它是为仅次于上或下一个动画移动到第二个动画,第二次previous.First时间点击做工精细,如果我以前的按钮,我得到了点击异常像,

Canvas: trying to use a recycled bitmap android.graphics.Bitmap 

请帮助我。

回答

2

如果您使用分辨率和内存较少的手机使用高分辨率图像,可能会发生这种情况。使用Drawable-hdpi,Drawable-mdpi,Drawable-ldpi文件夹并以合适的分辨率放置图像。

仅供参考..大多数情况下,您也可能在heapSize太少时在模拟器中看到此错误。尝试使用AVD管理器来增加堆大小

此链接可以帮助你 Supporting multiple screen resolutions

+0

我的图像大小为290x330.I认为这不是问题 – 2012-03-06 10:22:51

-2

,你可以用它来释放内存:

system.gc(); 
+1

它不叫GarbageCollector!它只是说“这将是很酷,如果垃圾收集器可以执行检查现在” – Sly 2012-03-06 10:32:24

0

看看下面的链接出来的内存已经讨论详细。

Android Out of memory issue

对我来说,托马斯的解决方案在过去工作过。

希望它有帮助。

+0

嗨德瓦,我添加了代码,请检查一下。 – 2012-03-06 12:00:25

+0

并没有帮助他的动画,其中包含预定义的xml框架 – Ewoks 2014-03-31 00:38:17

3

确切地说 - 系统知道何时收集垃圾,所以调用这个功能并没有什么帮助。

你必须真正管理你的应用程序内存。 290x330可能看起来不多,但如果您使用的是每像素4字节的完整RGBA,并且图像变为380K。如果你有其中几个,你会用尽内存。大多数Android设备不像个人电脑 - 它们的内存相当有限。有些人只有16M或24M来运行一切,包括操作系统和用户可能同时运行的任何其他应用程序。

您可以尝试包装你的日常使用尝试加载资源/捕获

While(! success) { 
    try { 
     // load resources 
    } catch (OutOfMemoryError E) { 
     // do something to use less memory and try again 
    } 
} 
+0

我添加了代码,请检查一次。 – 2012-03-06 11:59:47

相关问题