内存不足的错误,而使用位图多 [英] Out of Memory Error while using many bitmaps

查看:269
本文介绍了内存不足的错误,而使用位图多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用很多位图我的游戏画布。

I want use many bitmap for my game canvas.

我不能回收位图,因为我必须使用位图的整个部分。

I can't Recycle bitmaps, because I have to use bitmaps for the whole section.

不要提一些指导方针,以优化code处理许多位也为我的画布基于游戏应用更快的性能。

Do mention some guidelines to optimize the code to handle many bitmap also faster performance for my canvas based Game Application.

目前我使用以下code摆脱绘制资源位图

Currently i am using the following code to get bitmap from drawable resource,

    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
    bfOptions.inSampleSize = 1;
    bfOptions.inDither=false;                     //Disable Dithering mode
    bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    bfOptions.inTempStorage=new byte[16 * 1024]; 

    myBitMap=BitmapFactory.decodeResource(getResources(),ID, bfOptions);

我目前按照此 code 绘图在画布上。

图像是在sprite上以尝试加载许多精灵其给予的问题。

the images are in sprite so while trying to load many sprite its giving problem.

在此code链接的 MainGamePanel.java 是我在做什么完全吻合。位图解码我使用上面的方法。

In this code link MainGamePanel.java is what i am doing exactly . for bitmap decoding i use the above method.

推荐答案

有关真正有用的优化建议,这将是有益的了解正是你想要优化(绘图速度,内存占用)有什么更多的细节和你的游戏要求帆布应履行以及哪种图片,你想画(大的,小的,重复的,对它们进行breaked下来,被平铺可能)。

For really useful optimization proposals it would be beneficial to know more details about what exactly you want to optimize (drawing speed, memory footprint) and the requirements which your game canvas should fulfill as well as which kind of images you want to draw (big, small, repetitive, can they be breaked down, is tiling possible).

,你可以考虑春天我的脑海里有几个总体思路:

A few general thoughts which you could consider spring to my mind:


  • 你确定,你就无法再使用位图?我认为,在大多数游戏中,这是莫名其妙地可能的。也许你可以进一步划分您的图像?

  • 您可以缩放图像下来(或者,如果通过libsvg可能使用SVG图像 - 见 https://开头启动板。有关详细信息网/ libsvg-机器人

  • 如果您要优化所需要的图纸时,preloading您的图片可以改善这种

  • 您可以使用绘图线画出你的画布,而不是依赖于机器人自动重绘看法。对于上详见SurfaceHolder.lockCanvas(...)和SurfaceHolder.unlockCanvasAndPost(...)。

  • 如果您真的需要出色的绘图速度,可以考虑利用Open GL。

这篇关于内存不足的错误,而使用位图多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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