Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
制作一个有很多图像的android游戏,如何避免有很多图像的java.lang.OutOfMemory错误_Java_Android_Out Of Memory - Fatal编程技术网

制作一个有很多图像的android游戏,如何避免有很多图像的java.lang.OutOfMemory错误

制作一个有很多图像的android游戏,如何避免有很多图像的java.lang.OutOfMemory错误,java,android,out-of-memory,Java,Android,Out Of Memory,我一直在搜索Google、Stack Overflow和android开发者指南,但我找不到解决这个问题的好方法 我已经尝试过不绑定可提取项的方法: unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup)

我一直在搜索Google、Stack Overflow和android开发者指南,但我找不到解决这个问题的好方法

我已经尝试过不绑定可提取项的方法:

      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();
    }
 }
此外,每个活动都有一个740x1140 png背景,还有其他较小的敌人和按钮等图像视图。我一次在屏幕上看到的最多是8个。我通过XML分配所有这些图像。然而,在我开始一个新的活动之后,我完成了每一个活动,但似乎没有释放内存

下面是我的开始布局和日志

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bro_quest_start_bg"
android:orientation="vertical"
android:scaleType="centerCrop"
tools:context="com.torch2424.broquest.StartScreen" >

<ImageView
    android:id="@+id/broquesttitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/rrppgg_title"
    android:contentDescription="@string/tank" />

<Button
    android:id="@+id/start"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
    android:onClick="fight"
    android:layout_gravity="center_horizontal"
    android:text="@string/start" />

 <Button
    android:id="@+id/journal"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="journal"
    android:text="@string/journal" />

   <Button
    android:id="@+id/character"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="character"
    android:text="@string/character" />

<Button
    android:id="@+id/shop"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="shop"
    android:text="@string/shop" />


<Button
    android:id="@+id/options"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="options"
    android:text="@string/options" />

<Button
    android:id="@+id/quit"
     android:layout_width="120dp"
    android:layout_height="55dp"
     android:layout_gravity="center_horizontal"
    android:background="@drawable/buttons"
    android:onClick="quit"
    android:text="@string/quit" />

链接到Logcat,Log cat格式无法很好地处理堆栈溢出:(


p.S这些图像都是我的可绘制文件夹中的可绘制文件

尝试使用像毕加索这样的图像缓存库。

正如上面Voicu所说,我收到OOM错误,因为我的可绘制文件位于错误的文件夹中。我昨晚浏览互联网,在谷歌搜索结果中发现了这个问题:

其中,他描述了他如何将自己的图像从drawable文件夹移动到drawable nodpi文件夹,并解决了他所有的OOM问题。因此,我在我的应用程序中尝试了它,它也起到了作用。然而,图像看起来确实有点不同,所以为了弄清楚为什么会这样,让我们引用一些Android文档:

“drawable/中的资源是默认的可绘制资源。系统假定默认资源是针对基线屏幕大小和密度设计的,基线屏幕大小和密度为正常屏幕大小和中等密度。因此,系统会根据需要将默认密度资源向上扩展以用于高密度屏幕,向下扩展以用于低密度屏幕。”

因此,在我的例子中,我把“HD”手机的“HD”图像放在“SD”图像和手机的文件夹中,因为这也是drawables/默认值。因此,它反过来会缩放我所有的“HD”图像比实际大小和需要的大小更大,这不仅会使它们看起来更糟糕,还会使它们占用堆上更多的内存,从而导致我的应用程序中出现OOM(内存不足)错误

有关drawables文件夹和DPI的更多信息,请参阅Android文档的以下链接:


注意:每个活动的OnDestroy()中的Unbind Drawables都有助于提高内存,因此如果您有任何内存问题,我建议您尝试使用此方法。通过此调用,人们通常会调用System.gc()之后,这确实有帮助,但这是一种糟糕的编码实践。但在我的情况下,这就是/drawables。此外,我偶然发现tinypng.com减少了PNG大小,它对OOM错误毫无帮助,但它确实减少了apk大小,所以我建议你在游戏和其他方面这样做,因为它几乎不使用noti来减少文件大小明显不同。

这是在模拟器上吗?如果是的话,你应该在一台设备上试试。在logcat中,它说它在模拟器上。它是模拟器,但我也在我的android设备上试过,但我得到了相同的logcat。不过,游戏可以在崩溃之前运行更长时间。如果您将所有图像都放在默认的可绘制文件夹中,并希望您的应用程序能够在可能的高密度屏幕上正常运行,因为系统会在屏幕上放大图像。请尝试放置大小适当的图像(基于3:4:6:8的缩放比)在你的drawable-?dpi文件夹中。是的,谢谢你,Voicu!我昨晚刚刚弄明白了这一点,我打算在大约一个小时内详细回答我的问题,我在应用程序中测试了最后一件事。但你肯定是对的!我查看了它,它看起来不错!但我不知道我对使用第三方库的感觉如何,因为我计划出售游戏在某一点上,我知道许可证可能很棘手。但非常感谢你的评论!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bro_quest_start_bg"
android:orientation="vertical"
android:scaleType="centerCrop"
tools:context="com.torch2424.broquest.StartScreen" >

<ImageView
    android:id="@+id/broquesttitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/rrppgg_title"
    android:contentDescription="@string/tank" />

<Button
    android:id="@+id/start"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
    android:onClick="fight"
    android:layout_gravity="center_horizontal"
    android:text="@string/start" />

 <Button
    android:id="@+id/journal"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="journal"
    android:text="@string/journal" />

   <Button
    android:id="@+id/character"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="character"
    android:text="@string/character" />

<Button
    android:id="@+id/shop"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="shop"
    android:text="@string/shop" />


<Button
    android:id="@+id/options"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="options"
    android:text="@string/options" />

<Button
    android:id="@+id/quit"
     android:layout_width="120dp"
    android:layout_height="55dp"
     android:layout_gravity="center_horizontal"
    android:background="@drawable/buttons"
    android:onClick="quit"
    android:text="@string/quit" />