简体   繁体   English

Android drawables DPI

[英]Android drawables DPI

I have an Android app with 4 versions of the same image, in each of these folders: 我在以下每个文件夹中都有一个具有4个版本的同一图像的Android应用程序:

drawable-ldpi/ drawable-ldpi /
drawable-mdpi/ drawable-mdpi /
drawable-hdpi/ drawable-hdpi /
drawable-xhdpi/ drawable-xhdpi /

The only difference between the images is the size, I simply took the original large image and scaled it down according to the formula: 图像之间的唯一区别是大小,我只是简单地拍摄了原始的大图像,然后根据公式将其缩小:

120dp lpdi, 1 dp=0.75 px 120dp lpdi,1 dp = 0.75 px
160dp mpdi, 1 dp=1 px 160dp mpdi,1 dp = 1 px
240dp hdpi, 1 dp=1.5 px 240dp hdpi,1 dp = 1.5 px
320dp xhpdi, 1 dp=2 px 320dp xhpdi,1 dp = 2 px

The problem is I intend to have over 100 images, so to reduce the file size can't I just put the xhdpi versions in the drawables/ folder instead of having 4 versions of each image, then Android can scale the image as required? 问题是我打算拥有100张以上的图像,因此为了减小文件大小,我不能只将xhdpi版本放在drawables /文件夹中,而不是每个图像都有4个版本,那么Android可以根据需要缩放图像吗? What is the disadvantage of doing it this way? 这样做的缺点是什么? Is it simply a trade off between performance and file size? 是否只是在性能和​​文件大小之间进行权衡?

You can just put the xhdpi versions in the drawable resources. 您可以将xhdpi版本放入可绘制资源中。 Then in your layout/xml files specify the images dimensions in dp. 然后在您的layout / xml文件中以dp指定图像尺寸。 Android will downscale your images accordingly. Android会相应地缩小图像尺寸。 As long as you are downscaling the drawable resources quality of the image wont change. 只要您按比例缩小图像的可绘制资源的质量就不会改变。

having only the xhdpi resource will degrade A LOT your app performance. 仅具有xhdpi资源会降低您的应用程序性能。 Plus on low-range devices (small screens and small memory) it's very likely that the VM will run out of memory and crash whilst processing those large bitmaps. 此外,在低距离设备(小屏幕和小内存)上,VM在处理这些大位图时很可能会耗尽内存并崩溃。

You can develop everything on your high end device and use a simple photoshop automation to create the other resources but it's better to have a bigger APK than your app not working on mid and low range devices. 您可以在高端设备上开发所有内容,并使用简单的Photoshop自动化来创建其他资源,但是拥有一个更大的APK最好比不支持中低端设备的应用程序高。

Another option might be to export several APK (one for each screen type) and use the manifest to specify which screen sizes it does support and let the market filter the right APK to the right device. 另一个选择可能是导出几个APK(每种屏幕类型一个),并使用清单指定它支持的屏幕尺寸,然后让市场将正确的APK过滤到正确的设备上。

You can. 您可以。 The disadvantage may be slightly worse image quality: consider for hdpi display you need to scale 240/320 times, that is, 0.75, or to speak eg in 1D pixel line you make up 3 target pixels out of 4 source pixels. 缺点可能是图像质量稍差:考虑使用hdpi显示,您需要缩放240/320倍,即0.75,或者说一维像素行,您要在4个源像素中组成3个目标像素。 Most of the time it will look very good, but I imagine some pixel art may be an exception. 在大多数情况下,它看起来会很好,但我想有些像素艺术可能是个例外。 I guess this may be slower as well, and use more memory than it could, but I didn't measure that so I can't tell for sure. 我想这可能会更慢,并且会使用更多的内存,但是我没有进行测量,所以我不确定。

PS I'm using this method (providing xhdpi resources only) myself too. PS我自己也在使用这种方法(仅提供xhdpi资源)。

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

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