I keep getting a java.lang.outofmemory error
This question already has an answer here:
That 44k is compressed size. To use it as a background it is uncompressed into a bitmap. The memory size of a bitmap is the size of the color format for each pixel times the width times the height. Worse, if it is scaled, the system will need more memory to do the scaling as well.
So, for example, if you are using this color format: http://developer.android.com/reference/android/graphics/Bitmap.Config.html#RGB_565
Then the memory required for your bitmap is 2 x 2560 x 1600 = 7.8125 MB.
There is a manifest option for getting more memory: http://developer.android.com/guide/topics/manifest/application-element.html#largeHeap
Although you should also make sure to provide smaller resources for lower DPI devices and to only read in as much data as you need. The BitmapFactory class lets you skip pixels when reading in data if you don't need every pixel. The BitmapRegionDecoder lets you just read in the parts of an image you need.
Internally, your .png is represented with an integer per pixel (by default). An integer is 4 bytes. If you do the math:
2560 pixels * 1600 pixels * 4 bytes / pixel = 16,384,000 bytes = 15.625 MB
The memory limit per-app on older Android devices is 16 MB. The Portable Network Graphics format is typically compressed, and that's why there is a distinct difference in its file size and the size to represent the image in memory. Also, Android wastes a byte on alpha channel even if your image doesn't have or need an alpha channel.
Avoid using bitmaps to fill the background. They will not give the best results and will be scaled without preserving screen ratio.
Use 9-patches, shape drawables, simple colors and/or small bitmaps anchored to some corner.
链接地址: http://www.djcxy.com/p/93104.html上一篇: 正在出去