安卓:内存不够的异常/如何去codeResource添加到虚拟机的预算? [英] Android: Out of Memory Exception / How does decodeResource add to the VM Budget?

查看:198
本文介绍了安卓:内存不够的异常/如何去codeResource添加到虚拟机的预算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty的新的Andr​​oid,并已开发游戏。现在每一次我有用户报告出来了内存异常,我觉得奇怪,因为我创建位图最多200 KB的大小。我称之为 BitmapFactory.de codeResource()每当我创建一个新的精灵。由于我的应用程序是一个僵尸防御游戏,你可以期望,我创造精灵经常。

I am pretty new to Android and have been developing a game. Every now and again I have users reporting out of memory exceptions, which I find surprising since the bitmaps that I create are at most 200 kb in size. I call BitmapFactory.decodeResource() whenever I create a new sprite. Since my application is a zombie defense game, you could expect that I create sprites quite often.

每当我创建一个精灵,我打电话去code的资源生成位图。我的问题是,如果我只去调用code资源在每个活性对开始,并参考位在封装级,这将减少内存放在虚拟机预算是多少?

Every time I create a sprite, I call decode resource to generate a bitmap. My question is if I was to only call decode resource at the start of each activitiy, and refer to the bitmap at package level, would this lessen the amount of memory placed on the VM Budget?

推荐答案

  • c从像PNG图像资源的位图当你去$ C $,它 更多地取决于图像的尺寸而不是以KB大小。
  • 尝试,如果你可以减少原始图像的尺寸并没有真正影响你的输出。
  • 尝试重新使用位图而不是让它们解码
  • 探索更多的选择与BitmapFactory.Options()对象,例如增加inSampleSize可以减少存储器的图像所需的时间。 例如

  • When you decode the Bitmap from an image resource like png, it depends more on the dimensions of image rather then the size in KBs.
  • Try if you can reduce the dimensions of original image without really impacting your output.
  • Try reusing the bitmaps rather then keep decoding them.
  • Explore more options with BitmapFactory.Options() object, for example increasing inSampleSize can reduce the amount of memory required by the image. e.g

    
    BitmapFactory.Options o=new BitmapFactory.Options();
    o.inSampleSize = 4;
    o.inDither=false;                     //Disable Dithering mode
    o.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    myBitMap=BitmapFactory.decodeResource(getResources(),ID, o);
    

  • 在一个有用的技巧解决方案,如果没有其他的优化​​真的工作了你的是,你可以捕捉OutOfMemoryException异常,然后可以降低画质为最大..即inSampleSize设置为16,它会降低你的图像的质量但至少将节省您的应用程序崩溃,我在我的应用程序,我需要加载大量MP图像中的位图的人做了这一点。
  • 这篇关于安卓:内存不够的异常/如何去codeResource添加到虚拟机的预算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆