mipmap 目錄和drawable 目錄有什么區別


我簡單總結一下:

使用上沒有任何區別,你把它當drawable用就好了。

但是用mipmap系統會在縮放上提供一定的性能優化。

Android 在 API level 17 加入了 mipmap 技術,對 bitmap 圖片的渲染支持 mipmap 技術,來提高渲染的速度和質量。

mipmap 是一種很早就有的技術了,翻譯過來就是紋理映射技術。android 中的 mipmap 技術主要為了應對圖片大小縮放的處理,在android 中我們提供一個 bitmap 圖片,由於應用的需要(比如縮放動畫),可能對這個 bitmap 進行各種比例的縮小,為了提高縮小的速度和圖片的質量,android 通過 mipmap 技術提前對按縮小層級生成圖片預先存儲在內存中,這樣就提高了圖片渲染的速度和質量。

api 中通過 Bitmap 的 public final void setHasMipMap (boolean hasMipMap) 方法可以讓系統渲染器嘗試開啟 Bitmap 的 mipmap 技術。但是這個方法只能建議系統開啟這個功能,至於是否正真開啟,還是由系統決定。

res 目錄下面 mipmap 和 drawable 的區別也就是上面這個設置是否開啟的區別。mipmap 目錄下的圖片默認 setHasMipMap 為 true,drawable 默認 setHasMipMap 為 false。

 

官方介紹:

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.

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  # App icon used on Nexus 6 device launcher 

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.

ps------------------

谷歌官方:

drawable/

For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML 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.

谷歌官方強烈建議使用 mipmap 裝圖片,但是這兩段話對比,又有mipmap只是用來裝啟動圖的,其他圖片還是該裝到drawable里面的意思。好吧,所以英文不好的小主我跪了!!!最終大boss告訴小主,圖片放在drawable里面就好了。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM