Android項目中的mipmap文件夾是什麼?怎麼用?和drawable文件夾有什麼區別?

【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/ For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, andXML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type. mipmap/ For app launcher icons. The Android system retains the resources in this folder (and density-specific folders such as mipmap-xxxhdpi) regardless of the screen resolution of the device where your app is installed. This behavior allows launcher apps to pick the best resolution icon for your app to display on the home screen. For more information about using the 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.

這裏就解釋的更清楚了,之所以用mipmap,是因爲之前在適配unused screen densities時,會出現失真。而啓動圖標,放mipmap,則系統會選取最佳分辨率的圖片去顯示。


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 Bitmapwhen 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作爲bitmap(位圖)或者drawable的源(source),是一個簡單的方法提供高質量圖片和各種圖片縮放,特別是如果你想在動畫中縮放你的圖片時"。


先百度百科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比較穩妥,不然就坑大發了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章