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

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

问题描述

我有一个 Activity,其中我有一个 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 1536PNG 图像,它的重量约为 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天全站免登陆