您的位置:首页 > 移动开发 > Android开发

文章标题

2016-07-13 00:34 447 查看

关于Android中图片资源文件夹的一些理解

简单总结一下:

使用上没有任何区别,但是用mipmap系统会在缩放上提供一定的性能优化。

1. DPI这个概念

DPI,全称 dots per inch,意为每英寸的直线上像素点的数量。dpi越高,屏幕的画面越清晰,画质越细腻。

目前的Android设备支持以下几种DPI:

类别Dpi缩放比率代表机型
LDPI12075%
MDPI160100%
TVDPI213133%Nexus 7
HDPI160150%
XHDPI320200%
XXHDPI480300%
以Eclipse新建Android项目默认提供的机器人ICON为例:

2. 关于过去的Drawable资源存放原理

在drwable-hdpi的文件夹中,ic_launcher.png的size为72*72,而drawable-xhdpi文件夹中的ic_launcher.png的size为96*96。

理论上来说,在Sumsong Note2(5.5寸,1280*700,XHDPI)上,会加载drawable-xhdpi中的ic_launcher.png,96/320=0.3,使用者会看到一个0.3*0.3英寸的机器人ICON,而在Sumsong的Glaxy S2上(4.3寸,480*800,HDPI)上,会加载drawable-hdpi中到ic_launcher.png,72/240=0.3,使用者仍旧会看到一个0.3*0.3英寸的机器人ICON。

所以,从结果上来说,使用者在不同的设备上得到了相同到UI效果,而且,因为在Note2上使用了96*96的ICON, 可以获得更加精细的画面,这似乎是个很理想到结果:即维持了体验的一致性,又最大化的利用了屏幕的显示效果。

但是,理论和实际总是会有些微妙的差别。设备制造厂商为了迎合消费者的喜好,会生产各种屏幕尺寸的设备,而Android的归一化DPI只有6种(其中2种还是被市场淘汰的),最终呈现给开发者的结果就是,硬件设备的物理dpi(或者说ppi,pixel per inch)总是或大或小,和归一化dpi有一定差距,厂商会根据自己的需要设定设备的归一化DPI,而dalvik进而根据这个归一化DPI来加载资源,绘制界面。

Note2的物理分辨率其实是267ppi(其实,更接近HDPI,而非XHDPI),而非dalvik认为的320,96/267=0.36,所以,使用者实际看到的是一个0.36*0.36的ICON,而S2的物理分辨率是219,72/219=0.33,所以使用者实际看到的ICON为0.33*0.33寸。所以,界面的实际效果会和开发者的预想有一定偏差。

3. 基于DPI的资源加载优先级

首先,我们需要明白dalvik匹配最佳资源的策略,从Google的官方资料,我们可以知道dalvik是这样工作的:



详细的说明见这篇文章:Drawable资源介绍

这里仅列出最后的结论:

原则上来说,dalvik优先使用符合设备dpi的资源,其次是dpi较低的高dpi资源,再次是dpi较高的高dpi资源,最后采用nodpi的资源,由此,根据设备自身的dpi的不同,不同dpi资源的优先级是有差异的(忽略mdpi&hdpi)。

设备dpi优先级顺序(由高到低)
TVDPItvdpi> hdpi > xhdpi > xxhdpi > mdpi > default > ldpi > nodpi
HDPIhdpi > tvdpi > xhdpi > xxhdpi > mdpi> default > ldpi >nodpi
XHDPIxhdpi > xxhdpi > hdpi > tvdpi > mdpi > default > ldpi> nodpi
XXHDPIxxhdpi > xhdpi > hdpi > tvdpi >mdpi>default > ldpi > nodpi
另一篇墙裂推荐阅读文章:Android drawable微技巧

3. 关于Mipmap

官方介绍:

Mipmapping for drawables

Using a mipmap as the source for your bitmap or drawable is a simple way to provide a quality image and various image scales,which can be particularly useful if you expect your image to be scaled during an animation.

Android 4.2 (API level 17) added support for mipmaps in the Bitmap class—Android swaps the mip images in your Bitmap when you’ve supplied a mipmap source and have enabled setHasMipMap().

Now in Android 4.3, you can enable mipmaps for a BitmapDrawable object as well, by providing a mipmap asset and setting the android:mipMap attribute in a bitmap resource file or by calling hasMipMap().

应用场景:

If you know that you are going to draw this bitmap at less than 50% of its original size, you may be able to obtain a higher quality by turning this property on. Note that if the renderer respects this hint it might have to allocate extra memory to hold the mipmap levels for this bitmap.

一个应用实例:

Nexus 6

Screen

The Nexus 6 boasts an impressive 5.96” Quad HD screen display at a resolution of 2560 x 1440 (493 ppi). This translates to ~ 730 x 410 dp (density independent pixels).

Check your assets

It has a quantized density of 560 dpi, which falls in between the xxhdpi and xxxhdpi primary density buckets. For the Nexus 6, the platform will scale down xxxhdpi assets, but if those aren’t available, then it will scale up xxhdpi assets.

Provide at least an xxxhdpi app icon because devices can display large app icons on the launcher. It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. For example, an xxxhdpi app icon can be used on the launcher for an xxhdpi device.

 # App icon used on Nexus 6 device launcher

res/

 |-mipmap-mdpi/

 |----ic_launcher.png  

 |-mipmap-hdpi/

 |----ic_launcher.png

 |-mipmap-xhdpi/

 |----ic_launcher.png

 |-mipmap-xxhdpi/

 |----ic_launcher.png

 |-mipmap-xxxhdpi/

 |----ic_launcher.png

 

Choosing to add xxxhdpi versions for the rest of your assets will provide a sharper visual experience on the Nexus 6, but does increase apk size, so you should make an appropriate decision for your app.

res/

 |-drawable-mdpi/

 |----ic_sunny.png

 |-drawable-hdpi/

 |----ic_sunny.png

 |-drawable-xhdpi/

 |----ic_sunny.png

 |-drawable-xxhdpi/ #Fall back to these if xxxhdpi versions aren’t available

 |----ic_sunny.png

 |-drawable-xxxhdpi/ #Higher resolution assets for Nexus 6

 |----ic_sunny.png

总结:

这个实例总结一下是这样:

Nexus 6 有 493 ppi,它刚好在 xxhdpi和xxxhdpi之间,所以显示的时候需要对xxxhdpi的资源进行缩小,如果你用了mipmap-xxxhdpi,那么这里会对sclae有一个优化,性能更好,占用内存更少。所以现在官方推荐使用mipmap:

It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android