处理许多全屏png格式,以避免OOM [英] dealing with many full screen png to avoid OOM

查看:171
本文介绍了处理许多全屏png格式,以避免OOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我自己用的Arduino与沟通的应用程序。有自定义形状的按钮,所以我画他们PNG作为单一的背景,加入按钮的android:背景=@机器人:彩色/透明 使其更加透明化之上的自定义按钮差不多大小。

I have an app for my own use to communicate with arduino. There are custom shape buttons, so i drawn them in PNG as single background, added Buttons with android:background="@android:color/transparent" to make them transparent on top almost the size of custom buttons.

在此图片可以看到:1.背景 - 黑色背景绘制成单一的PNG深蓝色的按钮。 2.橙色PNG - 相同的尺寸为背景,用比赛parrent 选项绘制它。 3.隐形按钮 - 透明选项,蓝色正方形,有其中7

In this image you can see: 1. Background - black background with dark blue buttons drawn as single png. 2. Orange png - same size as background, drawing it with match parrent option. 3. Invisible button - with transparent option, blue square, there are 7 of them.

要消除指示图像绘制我他们背景相同的大小,PNG透明背景的错位。我将他们添加到 RES /绘制,并在布局XML作为的ImageView 的android:知名度= 水涨船高,布局的FrameLayout 。为了表明按钮被pressed我做设定可见橙色的自定义按钮,当Arduino的开启输出,发回确认我让我走了,并设置明显的另一个PNG 。它花费了大量的内存。图像尺寸为1478x2623。在最坏的情况下,一旦我有永久只显示8个图像+背景。而在onCreate方法我加载所有17图像。

To eliminate misplacing of indication images i drawn them same size of background in PNG transparent background. I added them to res/drawable and in layout XML as ImageView with android:visibility="gone", the Layout is FrameLayout. To indicate that button was pressed i make set VISIBLE of orange custom button, and when arduino turns on output, sends back confirmation i make i gone and set visible another PNG. It costs a lot of memory. The images dimensions are 1478x2623. At once in worst case i have to show only 8 images + background permanently. And in onCreate method i load all 17 images.

要避免OOM - 内存不足的错误,我要调整图像,这让我难看...

To avoid OOM - out of memory error, i have to resize images and it gives me bad looking...

如果在XML图像的能见度安卓知名度=水涨船高,它加载的onCreate的形象呢?如果是这样,我要创造,它的规模,并在需要时只画。如何做到这一点?

If visibility of image in XML is android:visibility="gone", does it load the image in onCreate? If so, i should create, scale it, and draw only when needed. How to achieve that?

修改

做了一些改动。


  1. 图片降低到1920×1080

  2. 8的ImageView留在布局和资源的方法设置,而不是在XML

这些变化作出更快,没有更多的OOM错误。此刻我很忙,因此需要一定的时间来测试答案建议的方法。我会尽力尽快

These changes made it faster and no more OOM error. At the moment i am very busy so it will take some time to test methods suggested in answers. I will try ASAP.

推荐答案

为什么你的图片这么大?该决议(1478x2623)是巨大的,在内存中保持8确实会导致OOM异常。

Why are your images so big? This resolution (1478x2623) is huge and keeping 8 in memory will indeed lead to OOM exceptions.

如果你真的必须,您可以执行以下操作:

If you really must, you can do either of the following:


  1. 廉价和肮脏的解决方案:使用大内存堆。由于这个程序是为您个人使用,这可能是你最快的选项,并会在你的清单变化的一行工作,但我不会推荐它对于一般的应用程序,除非有背后的原因,它需要更多的内存,真正的理由。 (为了实现这一点,请参见:<一href=\"http://stackoverflow.com/questions/11275650/how-to-increase-heap-size-of-an-android-application\">How增加一个Android应用程序的堆大小?)

  2. 使用UniversalImageLoadr( https://github.com/nostra13/Android-Universal-Image -Loader ),并确保你没有一次加载所有的图像。例如,只有当且仅当它需要装载显示您的影像重叠。你会通过设置图像源时,你需要和设置的不可见图像为null图像源做到这一点。此外,请确保您回收你不积极使用它们的位图。这其中大部分已经被由图像加载器库,虽然处理的。

  3. (在我看来,用这个和2):你真的不需要您指定的分辨率。它的方式过大。你应该看看Android指南( http://developer.android.com/training /displaying-bitmaps/index.html ),或者,使用图像加载器,并指定更小的尺寸和负载缩放图像。

  1. Cheap and nasty solution: Use large memory heap. Since this app is for your personal use, this might be the fastest option for you and will work with one line of change in your manifest but I wouldn't recommend it for general apps unless there's a real reason behind why it needs more memory. (To achieve this, see: How to increase heap size of an android application?)
  2. Use UniversalImageLoadr (https://github.com/nostra13/Android-Universal-Image-Loader) and make sure you do not load all your images at once. For instance, only load your image overlay if and only if it needs to be displayed. You'd do this by setting the image source when you need to and settings the image source for the invisible images to null. Additionally, make sure you recycle the bitmaps you aren't actively using them. Most of this is already being handled by the image loader library though.
  3. (Use this with 2 in my opinion): You really don't need the resolution you've specified. It's way too large. You should look at the Android guidelines (http://developer.android.com/training/displaying-bitmaps/index.html) or alternatively, use image loader and specify smaller dimensions and load scaled images.

要回答你的问题相关:
如果XML图像的可见性是安卓的知名度=水涨船高,它加载图像中的onCreate?如果是这样,我要创造,它的规模,并在需要时只画。如何做到这一点?

To answer your exact question: If visibility of image in XML is android:visibility="gone", does it load the image in onCreate? If so, i should create, scale it, and draw only when needed. How to achieve that?

是的,它仍然会加载图像,因为它需要在片刻的通知,以显示它是的,只有当你需要它,你应该去code位图。见上文第2点和3就如何实现这一目标。

Yes, it will still load the image since it will need to display it at a moment's notice and yes, you should decode bitmaps only when you need it. See above point 2 and 3 as to how to achieve this.

这篇关于处理许多全屏png格式,以避免OOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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