简体   繁体   English

最有效的显示图像的方式

[英]most efficient way of displaying images

My app uses 29 images in 2 different classes (and layouts). 我的应用程序使用2个不同类(和布局)中的29个图像。 In 1 layout there is 1 image view that changes between at most 25 of the images, and then the other layout is a scroll view that contains all images where at most 3 or 4 are visible on screen at a time. 在1个布局中,有1个图像视图在最多25个图像之间变化,然后另一个布局是滚动视图,其中包含一次最多3个或4个在屏幕上可见的所有图像。 Previously I had the images saved as .jpg and each is 720*540 and around 200-300kb in size. 以前我将图像保存为.jpg,每张图片大小为720 * 540,大小为200-300kb。 My app has had out of memory issues after going between my 2 layouts that are using the images; 在我使用图像的2个布局之间进行了我的应用程序内存不足问题; it is using over 100 mb. 它使用超过100 MB。 I did some research on bitmaps and tried them for my images, but they didn't help much because I still get out of memory issues and they increased the .apk size by almost 3 times. 我对位图进行了一些研究,并尝试将它们用于我的图像,但它们并没有多大帮助,因为我仍然遇到内存问题而且它们的.apk大小增加了近3倍。 What is the most preferred way to load images, is it as a drawable from a .jpg or from a bitmap or some other way? 加载图像的最佳方式是什么,它是从.jpg或位图或其他方式绘制的? Any suggestions would be appreciated. 任何建议,将不胜感激。

when the bitmap is in memory, it's uncompressed. 当位图在内存中时,它是未压缩的。 the original format doesn't matter. 原始格式无关紧要。 you can count on each image taking about width * height * 4 bytes of memory. 你可以指望每个图像采取宽度*高度* 4字节的内存。 eg, each of your 720x540 images will take ~1.5MB of memory. 例如,每个720x540图像需要大约1.5MB的内存。

it does not matter where you load them from, at least as far as the amount of memory required. 从哪里加载它们并不重要,至少就所需的内存量而言。

now read this, 现在看了,

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

You can read the source code of system Gallery, then you can totally understand how to operate bitmap. 您可以阅读系统库的源代码,然后您就可以完全理解如何操作位图。 When you get an image at first time, you have to decode the original file, and save it to cache file. 第一次获取图像时,必须解码原始文件,并将其保存到缓存文件中。 Then for the next time, you just decode the cache file, which is faster. 然后在下一次,您只需解码缓存文件,这更快。 Your memory is limited. 你的记忆力有限。 Don't expect to store all your bitmaps, just the bitmap that you wanna show on the screen. 不要指望存储所有位图,只是想要在屏幕上显示的位图。 The size of thumbnail is much smaller than full image. 缩略图的大小远小于完整图像。 So there is no problem if you wanna put 8 ~ 10 thumbnails on screen. 如果你想在屏幕上放置8~10个缩略图,那就没问题了。 When users click the thumbnail to browse the full image, you can load the full image into memory, and recycle other bitmaps. 当用户单击缩略图以浏览完整图像时,您可以将完整图像加载到内存中,并回收其他位图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM