为什么我所有的位图上采样后的200%? [英] Why are all my bitmaps upsampled 200%?

查看:132
本文介绍了为什么我所有的位图上采样后的200%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序内存严重问题[1]。为了研究这个,我把我的应用程序的heapdumps在不同的状态。我看到一些位图正在采取大量内存。我写了一个小工具[2]德codeS的字节数组到Windows位图文件(.BMP),这样我就可以的的位图,并将它们与我在文件我 RES /绘制文件夹。

我发现的是,我的所有文件都上采样两次。
我先用最大的一个检查有:超过9MB堆中的字节数组缓冲区,这是德codeD是一个不错的1920x1280的画面,而原来一个是960x640的PNG文件
。 我试图与第二大,超过3MB,曾经去​​codeD表现出了不错的754x1200的图片,原来的大小......你猜怎么着?一个漂亮的377x600 JPG文件。

怎么办?

我已经启用硬件加速在我的Andr​​oid清单文件(虽然我不知道我真的需要它,我只是用一些基本观点和活动)。
我在一个GSM Galaxy Nexus的(yakju)运行普通的Andr​​oid 4.0.2。我收到从我的测试人员的反馈,问题是他们的4.0.3的Nexus S present,虽然我无法检查其堆转储呢。

我试图以节省内存在这里,如果Android的双打一切,难怪应用程序崩溃,很快,因为堆的使用量过高(约64MB在我的情况)。我希望有一个原因和变通的办法。

参考文献:

  1. <一个href="http://stackoverflow.com/questions/9536521/outofmemoryerror-when-loading-activities">OutOfMemoryError装载活动时,
  2. <一个href="http://stackoverflow.com/questions/9611592/how-to-actually-see-a-bitmap-taken-from-an-android-heap-dump">How实际上看到一个Android堆转储
  3. 采取了位图
解决方案

当你把图像 RES /绘制,Android将假定他们有一个DPI为160,即它是一样的把它们放在 RES /绘制-MDPI 。 Galaxy Nexus的是xhdpi设备,也就是说,它具有320一(广义)DPI为了弥补高分辨率显示,Android将上采样图像200%。

解决方法很简单,只要把图像 RES /绘制-xhdpi 代替。然后将图像的声明dpi将匹配运行上的显示,而Android将不执行任何影像缩放。

请参阅<一个href="http://developer.android.com/guide/practices/screens_support.html">http://developer.android.com/guide/practices/screens_support.html获取更多信息。

I'm having severe memory issues in my application [1]. In order to investigate this, I took heapdumps of my app at different states. I saw that some bitmaps were taking huge amounts of memory. I wrote a small tool [2] that decodes the byte arrays to Windows bitmap files (.bmp), so that I can see the bitmaps and compare them to the files I have in my res/drawable folder.

What I discovered is that all my files are upsampled twice.
I first checked with the biggest one had: a byte array buffer of more than 9MB in the heap, which was decoded to be a nice 1920x1280 picture, while the original one was a 960x640 png file.
I tried with the second biggest, over 3MB, which once decoded showed a nice 754x1200 picture, the original size was... guess what? A nice 377x600 jpg file.

What gives?

I have enabled HW acceleration in my Android Manifest file (though I'm not sure I really need it, I'm just using some basic views and activities).
I'm running stock Android 4.0.2 on a GSM Galaxy Nexus (yakju). I'm receiving feedback from my testers that the issue is present on their 4.0.3 Nexus S, though I couldn't check their heap dumps yet.

I'm trying to save memory here, if Android doubles everything, no wonder the app crashes quickly because the heap usage gets too high (around 64MB in my case). I hope there's a reason and a way around it.

References:

  1. OutOfMemoryError when loading activities
  2. How to actually see a Bitmap taken from an Android heap dump

解决方案

When you put images in res/drawable, Android will assume they have a dpi of 160, i.e. it is the same as putting them in res/drawable-mdpi. Galaxy Nexus is an xhdpi device, i.e. it has a (generalized) dpi of 320. To compensate for the high resolution display, Android will upsample the images with 200%.

The solution is simple, just put the images in res/drawable-xhdpi instead. Then the declared dpi of the images will match the display you run on, and Android will not perform any scaling of the images.

Please refer to http://developer.android.com/guide/practices/screens_support.html for more info.

这篇关于为什么我所有的位图上采样后的200%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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