使用9补丁图像是否会节省大量内存? [英] Will it save a lot of memory by using 9-patch images?

查看:85
本文介绍了使用9补丁图像是否会节省大量内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与应用程序中的OutOfMemoryError对抗.

I am fighting against OutOfMemoryError in my app.

我创建了800像素x 480像素的背景图像.当将此图像加载到使用它作为背景的视图中时,我认为操作系统将为其使用800 * 480 * 4字节.内存很大.

I created a background image, which is 800 pixel x 480 pixel. When this image is loaded into the view that uses it as background, I think the OS will use 800*480*4 bytes for it. It is a lot of memory.

如果我创建一个10像素x 10像素的9色块图像来替换整个屏幕图像.操作系统将渲染使用9色块的视图时,会将9色块图像自动缩放为800x480.我的问题是,在9修补程序的情况下,操作系统将使用多少内存来绘制缩放的9修补程序图像?是10 * 10 * 4字节还是800 * 480 * 4字节?

If I create a 10 pixel x 10 pixel 9-patch image to replace the whole screen image. The OS will auto-scale the 9-patch image to 800x480 when it renders the view that uses the 9-patch. My question is that, in the 9-patch case, how much memory will OS use to draw the scaled 9-patch image? will it be 10*10*4 bytes or 800*480*4 bytes?

谢谢.

推荐答案

首先,如果它是背景图像并且可以缩放,请这样做,因为众所周知这是最佳做法(尤其是背景),并且选择正确的颜色和/或背景图案可以弥补轻微的图像清晰度损失.

Firstly, if it is a background image, and could be scaled, please do so, as it is known to be the best practice (especially for backgrounds) and the slight loss of image clarity could be compensated by choosing the correct colours and/or background pattern.

关于内存,如果您使用的是Drawable,那么您是安全的.但是,位图显然不是通过标准Java方式分配的,而是通过本机调用分配的.分配是在虚拟堆之外完成的,但会根据虚拟堆进行计数.有关此问题的更多信息

Regarding memory, if you are using Drawable you are on the safe side. But the Bitmaps are apparently not allocated in a standard Java way but via native calls; the allocations are done outside of the virtual heap, but are counted against it. More on this problem here

这篇关于使用9补丁图像是否会节省大量内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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