Android:在ImageView中使用大图像的性能降低 [英] Android: Slow performance using a big image in ImageView

查看:514
本文介绍了Android:在ImageView中使用大图像的性能降低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动我在ImageView中有一个背景图像,图像顶部有九个透明按钮。

I have an Activity where I have a background image in an ImageView, and nine transparent buttons on top of the image.

http://i.stack.imgur.com/GGQqd.png

当我点击任何按钮时,我会在后台顶部显示 RelativeLayout

When I tap any of the buttons I make a RelativeLayout appears in the top of the background.

http://i.stack.imgur。 com / JvKQK.jpg

问题是:当我使用大分辨率图像作为布局中的背景时,性能非常差。

The problem is: When I use a big resolution image as a background in the layout the performance is very poor.

我正在使用 2048 x 1536 PNG 图片,它的重量约为500k。
当我使用较小的图像,如 1024 x 768 时,应用程序运行正常,但看起来很糟糕。

I'm using a PNG image of 2048 x 1536, and it's weight is about 500k. When I use a smaller image like 1024 x 768 the application works fine, but it looks terrible.

是否有限制或方法使用大图像而不会失去性能?

Is there a restriction or a method to use big images without losing performance?

推荐答案

该位图巨大即可。你提到的500k是压缩大小。使用它们时,图像在内存中未压缩。您实际上看的是2048×1536×4 = 12582912 =超过12MB的内存使用。

That bitmap is huge. The 500k you mention is the compressed size. Images are uncompressed in memory when you use them. You're actually looking at 2048×1536×4 = 12582912 = over 12MB of memory use.

如果您使用的是具有该屏幕分辨率的Nexus 9设备,那么你可以合理地假设它还有内存,GPU和总线带宽来处理那个大小的图像。但是,如果您使用的是较低分辨率的设备(大多数设备都要记住,即使是全高清也只有65%),那么这个图像显然会浪费内存。你现在可以购买240x320屏幕的低端设备,其中全屏位图仅为你图像大小的2.5%。

If you're on a device like Nexus 9 that has that screen resolution, then you can reasonably assume it also has the memory, GPU and bus bandwidth to deal with an image of that size. However, if you're on a lower resolution device (most devices are, keep in mind that even Full HD is just 65% as large), then this image is phenomenally wasteful of memory. You can buy low end devices today with a 240x320 screen, where a full screen bitmap is a mere 2.5% the size of your image.

你将不得不加载图像时缩放图像。使用 BitmapFactory.Options 在加载图片之前设置所需的尺寸。

You're going to have to scale that image when you load it. Use BitmapFactory.Options to set the desired size before loading the image.

此外,您直接放置文字在它的上面。文本渲染需要Alpha透明度,这需要对底层图像进行采样。如果您可以将文本放在不透明的背景上并将其置于顶部,那么您也将节省一些GPU负载,但我实际上并不确定能够获得多少性能。这可能不是什么大不了的事。

Furthermore, you're putting text directly on top of it. Text rendering requires alpha transparency, which requires sampling the underlying image. If you can put the text on an opaque background and put that on top, you'll save some GPU load as well, but I'm actually not sure how much performance that's going to get you. It may not be so big a deal.

这篇关于Android:在ImageView中使用大图像的性能降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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