【2015年10月10日 星期六】
很多人问这个,而百度知道第一个答案说是“谷歌官方强烈推荐mipmap来装图片,以替代drawable”,我原本信以为真(其他很多答案也是类似,说mipmap文件夹是用来替代drawable文件夹的)。
但后来有人提出异议,我才上官网查了一下,发现谷歌压根并没说过这句话。
官网压根没说用mipmap文件夹替代drawable文件夹,以下是我在安卓官网找到的有提到mipmap文件夹的文章(而且Android官网对mipmap文件夹的介绍比较少。)。
(结论:就目前来说,mipmap文件夹只是用来放启动图标的,AndroidStudio新建项目的ic_launcher.png都是默认放在mipmap文件夹下的。)
1、第一处:
首先是"Android Studio 1.1 Preview"的介绍中有提到,(原文http://tools.android.com/recent/androidstudio11preview1released)
New projects now create launcher icons as @mipmap resources instead of @drawable (see http://android-developers.blogspot.com/2014/10/getting-your-apps-ready-for-nexus-6-and.html for more)
意思就是新项目的“启动图标”(launcher icons)现在使用@mipmap来替代原先的@drawable。
而链接的那篇文章,《Getting Your Apps Ready for Nexus 6 and Nexus 9》,说的也是launcher icon的事情,并没有提到替代Drawable放位图什么的。
2、第二处:
Android tool的管理项目篇(Managing Projects Overview)(地址http://developer.android.com/tools/projects/index.html)
此文说明了项目中各个文件夹的作用,关于res文件夹,文中这么说:“
drawable/
mipmap/
mipmap
folders, see
Managing Launcher Icons as mipmap Resources.
”
这段文字的意思即,
drawable用于存放位图文件(PNG、JPEG、GIF),还有点九图片和XML文件(shape和selector等)。
而mipmap是用来存放app launcher icons,即启动图标,无论何种屏幕分辨率,系统都会选择最适合的分辨率的icon显示在主屏上。
3、第三处:
即第二处文章末尾链接的文章:(地址:http://developer.android.com/tools/projects/index.html#mipmap)
Managing Launcher Icons as mipmap Resources
Different home screen launcher apps on different devices show app launcher icons at various resolutions. When app resource optimization techniques remove resources for unused screen densities,launcher icons can wind up looking fuzzy because the launcher app has to upscale a lower-resolution icon for display. To avoid these display issues , apps should use the mipmap/
resource folders for launcher icons. The Android system preserves these resources regardless of density stripping, and ensures that launcher apps can pick icons with the best resolution for display.
Make sure launcher apps show a high-resolution icon for your app by moving all densities of your launcher icons to density-specific res/mipmap/
folders (for example res/mipmap-mdpi/
and res/mipmap-xxxhdpi/
). The mipmap/
folders replace the drawable/
folders for launcher icons. For xxhpdi launcher icons, be sure to add the higher resolution xxxhdpi versions of the icons to enhance the visual experience of the icons on higher resolution devices.
Note: Even if you build a single APK for all devices, it is still best practice to move your launcher icons to the mipmap/
folders.
4、第四处:
https://developer.android.com/about/versions/android-4.3.html(StackOverflow里提到的)
讲的是Android4.3的API,其中有提到mipmap,标题是《Mipmapping for drawables》,原文如下:
引用最广的回答:http://segmentfault.com/q/1010000002603418/a-1020000002603498,里面引用的也是下面这段内容。
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()
.
先百度百科mipmap的意思:
“mipmap指的是一种纹理映射技术,Willams将低一级图像的每边的分辨率取为高一级图像的每边的分辨率的二分之一,而同一级分辨率的纹理组则由红、绿、蓝三个分量的纹理数组组成。由于这一个查找表包含了同一纹理区域在不同分辨率下的纹理颜色值,因此被称为Mipmap。”
这文章也有解释,http://blog.csdn.net/linber214/article/details/3342051/),内容如下:
Mipmap纹理技术是目前解决纹理分辨率与视点距离关系的最有效途径,它会先将图片压缩成很多逐渐缩小的图片,
例如一张64*64的图片,会产生64*64,32*32,16*16,8*8,4*4,2*2,1*1的7张图片,
当屏幕上需要绘制像素点为20*20 时,
程序只是利用 32*32 和 16*16 这两张图片来计算出即将显示为 20*20 大小的一个图片,
这比单独利用 32*32 的那张原始片计算出来的图片效果要好得多,速度也更快.
结合mipmap的意思,我猜测是说Bitmap可以通过setHasMip()和android:mipmap来使用mipmap技术,而在Android4.2中,Bitmap类已经添加了对mipmaps的支持。我猜测,mipmap大概是Android系统为了解决不同分辨率显示高清图标而采用的一个技术,系统或许会按照上面的技术,来使图标保证清晰且适配各种屏幕。而mipmap也可以用在需要在动画中被缩放的图片,应该也是用到了同样的原理。
那么是不是图片放在mipmap文件夹下,和setHasMip()、android:mipmap是同样的效果呢?这里就不是很清楚了。
但是,就目前来说,官网只说明,mipmap用于存放启动图标,而PNG等位图、点九图、XML,还是放在drawable。所以
-------分割线----------
最后贴上stackoverflow的该问题的解答。
http://stackoverflow.com/questions/23935810/mipmap-drawables-for-icons。
在stackoverflow中也有人问这个问题,但是回答非常的详尽,有兴趣的可以去看看。
那么多回答中,我没找到谁说用mipmap文件夹取代drawable文件夹。
而且,也说明,百度知道的答案未必靠谱,尽量还是去StackOverflow比较稳妥,不然就坑大发了。