What's mipmap-anydpi-v26 in res directory in A

2020-02-17 09:56发布

In Android Studio 3.0, once we create a project, a folder named mipmap-anydpi-v26 is automatically created in the res directory. What actually does it do? Why do we need it? How will we utilize it for development purposes?

Also, there are two XML files automatically created in this folder after project setup. Why do these XML files reside in a mipmap folder? I thought we should keep all XML files in a drawable folder instead of mipmap.

3条回答
做个烂人
2楼-- · 2020-02-17 10:26

I have found an explanation about this, here is some context:

To add an adaptive icon that replaces all PNGs on API 26+ devices, you’ll add a res/mipmap-anydpi-v26/ic_launcher.xml file that looks like this:

<adaptive-icon
    xmlns:android="http://schemas.android.com/apk/res/android">
  <background android:drawable="@drawable/ic_launcher_background"/>
  <foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

By placing it in the mipmap-anydpi-v26 folder, the resource system will use it in preference over any files in the other dpi folders (exactly what you want, since this file is replacing all of them) and that it should only be used in API 26+ devices.

查看更多
贪生不怕死
3楼-- · 2020-02-17 10:28

Android Studio 3 creates an adaptive icon for your app which is only available in SDK 26 and up. Launcher icons should be put into the mipmap folders.

If you look at your manifest, you can see that it references ic_launcher

android:icon="@mipmap/ic_launcher"

If you look in your mipmap folder, you see your normal 5 different launcher icons which will be used for anything lower than SDK 26. For SDK 26 and up, it uses the XML files from the anydpi-v26 folder to make use of adaptive icon.

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
查看更多
家丑人穷心不美
4楼-- · 2020-02-17 10:31

Both answers above give pretty good summary of what mipmap-anydpi-v26 folder does but I feel why part is missing. So heres my 2 cents.

anydpi: These resources take precedence in any dpi. So even if you have mipmap-hdpi or mipmap-mdpi matching with current devices density, resource from mipmap-anydpi will always be picked up.

anydpi-v26: This is an additional filter over just anydpi. This says resources will always be picked up from anydpi-v26 regardless of devices density only if SDK/API level is 26 or higher(Oreo).

So you can have mipmap-anydpi-v26 or drawable-anydpi-v26. All resource folders will follow above logic.

Now that we know the answer of "why mipmap-anydpi-v26"? lets try to understand why "mipmap-anydpi-v26/ic_launcher.xml".

This is because ic_launcher.xml is used to describe adaptive icon for your app which is only available in SDK 26 and up as mentioned by other answers. Hope this helps.

查看更多
登录 后发表回答