Android 图标的 Mipmap 可绘制对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23935810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:38:54  来源:igfitidea点击:

Mipmap drawables for icons

androidandroid-drawablemipmaps

提问by Richard Le Mesurier

Since Android 4.3 (Jelly Bean) we can now make use of the res/mipmapfolders to store "mipmap" images.

从 Android 4.3 (Jelly Bean) 开始,我们现在可以使用res/mipmap文件夹来存储“mipmap”图像。

For example, Chrome for Androidstores its icons in these folders instead of the more normal res/drawablefolders.

例如,Android 版 Chrome会将其图标存储在这些文件夹中,而不是更普通的res/drawable文件夹中。

How are these mipmap images different from the other familiar drawable images?

这些 mipmap 图像与其他熟悉的可绘制图像有何不同?

I see that in my manifest, we use the @mipmap/qualifier, instead of @drawable/, which makes sense given the resource folder name:

我看到在我的清单中,我们使用了@mipmap/限定符,而不是@drawable/,鉴于资源文件夹名称,这是有道理的:

<activity
    android:name=".MipmapDemp"
    android:icon="@mipmap/ic_launcher" />


References:

参考:

The Android 4.3 APIsdocument has the following to say:

采用Android 4.3的API文档具有以下的说:

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 作为位图或 drawable 的源是一种提供高质量图像和各种图像比例的简单方法,如果您希望在动画期间缩放图像,这将特别有用。

Android 4.2(API 级别 17)在 Bitmap 类中添加了对 mipmap 的支持——当您提供了 mipmap 源并启用了 setHasMipMap() 时,Android 会交换您的 Bitmap 中的 mip 图像。现在,在 Android 4.3 中,您也可以通过提供 mipmap 资产并在位图资源文件中设置 android:mipMap 属性或调用 hasMipMap() 来为 BitmapDrawable 对象启用 mipmap。

I don't see anything in there that helps me to understand.

我在那里看不到任何可以帮助我理解的内容。



XML Bitmap resourceshave an android:mipMapproperty:

XML 位图资源有一个android:mipMap属性:

Boolean. Enables or disables the mipmap hint. See setHasMipMap() for more information. Default value is false.

布尔值。启用或禁用 mipmap 提示。有关更多信息,请参阅 setHasMipMap()。默认值为假。

This does not apply to launcher icons as far as I can see.

据我所知,这不适用于启动器图标。



The question was raised on Google Groups (The purpose of resource name "mipmap"?!), to which Romain Guy replied:

这个问题是在 Google Groups 上提出的(资源名称“mipmap”的目的?!),Romain Guy 回答说:

It's useful to provide an image at a larger resolution that would normally be computed (for instance, on an mdpi device, Launcher might want the larger hdpi icon to display large app shortcuts.)

以通常会计算的较大分辨率提供图像很有用(例如,在 mdpi 设备上,Launcher 可能希望较大的 hdpi 图标显示大型应用程序快捷方式。)

I feel like this almost makes sense of it, but not quite.

我觉得这几乎是有道理的,但并不完全。

I'm still inclined to go with Randy Sugianto's follow up:

我仍然倾向于使用 Randy Sugianto 的跟进:

What are the advantages of this? Is there any guide how to use mipmaps, probably for better launcher icons?

这有什么好处?是否有任何指南如何使用 mipmap,可能是为了更好的启动器图标?



Of course, Wikipedia has a page for "Mipmap", which refers to an older technique invented in 1983, that I can't quite relate to the current Android implementation.

当然,维基百科有一个“Mipmap”页面,它指的是 1983 年发明的一种较旧的技术,我无法与当前的 Android 实现完全相关。



Should we be storing all our app icons in res/mipmapfoldersthese days, and what are the guidelinesfor these mipmap images?

这些天我们是否应该将所有应用程序图标res/mipmap存储文件夹中,这些 mipmap 图像的准则是什么?



Update #1

更新 #1

Here's a blog post that tries to explain it a bit.

这是一篇博客文章,试图对其进行一些解释。

But the image used in that blog post shows what looks like one file with many logos in it. This is not what I see in Chrome's mipmap folder.

但是该博客文章中使用的图像显示了一个文件,其中包含许多徽标。这不是我在 Chrome 的 mipmap 文件夹中看到的。

Chrome's mipmap-hdpifolder contains three images. One is the Chrome logo, on its own.

Chrome 的mipmap-hdpi文件夹包含三个图像。一个是 Chrome 徽标,单独使用。

Chrome mipmap-hdpi icon.png

Chrome mipmap-hdpi icon.png

Strangely, it is 72x72, not 48x48 which I would expect to see.

奇怪的是,它是 72x72,而不是我希望看到的 48x48。

Perhaps that is all there is to this - we just need to keep bigger icons in the mipmap folders?

也许这就是全部 - 我们只需要在 mipmap 文件夹中保留更大的图标?



Update #2

更新 #2

The Android Developers Blog post of 23/10/2014 again confirms the idea of using the mipmapfolders for application icons:

2014 年 10 月 23 日的 Android 开发者博客文章再次确认了使用mipmap文件夹作为应用程序图标的想法:

When talking about the Nexus 6 screen density, the author writes:

在谈到 Nexus 6 屏幕密度时,作者写道:

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.

最佳做法是将您的应用程序图标放在 mipmap- 文件夹(而不是 drawable- 文件夹)中,因为它们的使用分辨率与设备的当前密度不同。例如,可以在 xxhdpi 设备的启动器上使用 xxxhdpi 应用程序图标。



Update #3

更新 #3

Note that Android Studio creates the ic_launcher.pngicons in the mipmap...folders rather than the drawable...folders that Eclipse used to create them in.

请注意,Android Studioic_launcher.pngmipmap...文件夹中创建图标,而不是drawable...Eclipse 用于在其中创建图标的文件夹。



采纳答案by Kevin TeslaCoil

There are two distinct uses of mipmaps:

mipmap 有两种不同的用途:

  1. For launcher icons when building density specific APKs. Some developers build separate APKs for every density, to keep the APK size down. However some launchers (shipped with some devices, or available on the Play Store) use larger icon sizes than the standard 48dp. Launchers use getDrawableForDensity and scale down if needed, rather than up, so the icons are high quality. For example on an hdpi tablet the launcher might load the xhdpi icon. By placing your launcher icon in the mipmap-xhdpi directory, it will not be stripped the way a drawable-xhdpi directory is when building an APK for hdpi devices. If you're building a single APK for all devices, then this doesn't really matter as the launcher can access the drawable resources for the desired density.

  2. The actual mipmap API from 4.3. I haven't used this and am not familiar with it. It's not used by the Android Open Source Project launchers and I'm not aware of any other launcher using.

  1. 用于构建特定于密度的 APK 时的启动器图标。一些开发人员为每个密度构建单独的 APK,以减小 APK 的大小。但是,某些启动器(随某些设备一起提供,或在 Play 商店中提供)使用比标准 48dp 更大的图标尺寸。启动器使用 getDrawableForDensity 并根据需要缩小而不是放大,因此图标质量很高。例如,在 hdpi 平板电脑上,启动器可能会加载 xhdpi 图标。通过将启动器图标放在 mipmap-xhdpi 目录中,它不会像 drawable-xhdpi 目录那样在为 hdpi 设备构建 APK 时被剥离。如果您正在为所有设备构建一个 APK,那么这并不重要,因为启动器可以访问所需密度的可绘制资源。

  2. 来自 4.3 的实际 mipmap API。这个我没用过,也不熟悉。它没有被 Android 开源项目启动器使用,我不知道有任何其他启动器在使用。

回答by Kazuaki

It seems Google have updated their docssince all these answers, so hopefully this will help someone else in future :) Just came across this question myself, while creating a new (new new) project.

自从所有这些答案以来,谷歌似乎已经更新了他们的文档,所以希望这会在未来帮助其他人:) 在创建一个新的(新的)项目时,我自己刚刚遇到了这个问题。

TL;DR: drawables may be stripped out as part of dp-specific resource optimisation. Mipmaps will not be stripped.

TL;DR:作为特定于 dp 的资源优化的一部分,可绘制对象可能会被剥离。Mipmap 不会被剥离。

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.

不同设备上的不同主屏幕启动器应用程序以不同的分辨率显示应用程序启动器图标。当应用程序资源优化技术为未使用的屏幕密度移除资源时,启动器图标可能看起来很模糊,因为启动器应用程序必须放大较低分辨率的图标以进行显示。为避免这些显示问题,应用程序应将mipmap/资源文件夹用于启动器图标。无论密度剥离如何,Android 系统都会保留这些资源,并确保启动器应用程序可以选择具有最佳分辨率的图标进行显示。

(from http://developer.android.com/tools/projects/index.html#mipmap)

(来自http://developer.android.com/tools/projects/index.html#mipmap

回答by sergej shafarenka

How are these mipmap images different from the other familiar drawable images?

这些 mipmap 图像与其他熟悉的可绘制图像有何不同?

Here is my two cents in trying to explain the difference. There are two cases you deal with when working with images in Android:

这是我试图解释差异的两分钱。在 Android 中处理图像时,您会遇到两种情况:

  1. You want to load an image for your device density and you are going to use it "as is", without changing its actual size. In this case you should work with drawablesand Android will give you the best fitting image.

  2. You want to load an image for your device density, but this image is going to be scaled up or down. For instance this is needed when you want to show a bigger launcher icon, or you have an animation, which increases image's size. In such cases, to ensure best image quality, you should put your image into mipmapfolder. What Android will do is, it will try to pick up the image from a higher density bucket instead of scaling it up. This will increase sharpness (quality) of the image.

  1. 您想为您的设备密度加载图像,并且您将“按原样”使用它,而不更改其实际大小。在这种情况下,您应该使用可绘制对象,Android 将为您提供最合适的图像。

  2. 您想为您的设备密度加载图像,但该图像将被放大或缩小。例如,当您想显示更大的启动器图标时,或者您有动画,这会增加图像的大小时,就需要这样做。在这种情况下,为了确保最佳图像质量,您应该将图像放入mipmap文件夹。Android 会做的是,它会尝试从更高密度的存储桶中提取图像,而不是将其放大。这将增加图像的清晰度(质量)。

Thus, the rule of thumb to decide where to put your image into would be:

因此,决定将图像放入何处的经验法则是:

  • Launcher iconsalways go into mipmapfolder.
  • Images, which are often scaled up(or extremely scaled down) and whose quality is critical for the app, go into mipmapfolder as well.
  • All other imagesare usual drawables.
  • 启动器图标总是进入mipmap文件夹。
  • 图像通常被放大(或极度缩小)并且其质量对应用程序至关重要,也会进入mipmap文件夹。
  • 所有其他图像都是通常的可绘制对象

回答by matiash

The Android implementation of mipmaps in 4.3 is exactly the technique from 1983 explained in the Wikipedia article :)

4.3 中 mipmap 的 Android 实现正是维基百科文章中解释的 1983 年的技术:)

Each bitmap image of the mipmap set is a downsized duplicate of the main texture, but at a certain reduced level of detail. Although the main texture would still be used when the view is sufficient to render it in full detail, the renderer will switch to a suitable mipmap image (...) when the texture is viewed from a distance or at a small size.

mipmap 集的每个位图图像都是主纹理的缩小副本,但细节水平有所降低。尽管当视图足以渲染它的全部细节时仍会使用主纹理,但当从远处或以小尺寸查看纹理时,渲染器将切换到合适的 mipmap 图像 (...)。

Although this is described as a technique for 3D graphics (as it mentions "viewing from a distance"), it applies just as well to 2D (translated as "drawn is a smaller space", i.e. "downscaled").

尽管这被描述为一种用于 3D 图形的技术(因为它提到了“从远处观看”),但它也同样适用于 2D(翻译为“绘制的空间较小”,即“缩小”)。

For a concrete Android example, imagine you have a View with a certain background drawable (in particular, a BitmapDrawable). You now use an animation to scale it to 0.15 of its original size. Normally, this would require downscaling the background bitmap for each frame. This "extreme" downscaling, however, may produce visual artifacts.

对于一个具体的 Android 示例,假设您有一个带有特定背景可绘制对象(特别是 a BitmapDrawable)的视图。您现在可以使用动画将其缩放到其原始大小的 0.15。通常,这需要缩小每一帧的背景位图。然而,这种“极端”缩小可能会产生视觉伪像。

You can, however, provide a mipmap, which means that the image is already pre-rendered for a few specific scales (let's say 1.0, 0.5, and 0.25). Whenever the animation "crosses" the 0.5 threshold, instead of continuing to downscale the original, 1.0-sized image, it will switch to the 0.5 image and downscale it, which should provide a better result. And so forth as the animation continues.

但是,您可以提供 mipmap,这意味着图像已经针对一些特定比例(例如 1.0、0.5 和 0.25)进行了预渲染。每当动画“越过”0.5 阈值时,它不会继续缩小原始 1.0 大小的图像,而是切换到 0.5 图像并缩小它,这应该会提供更好的结果。随着动画的继续,依此类推。

This is a bit theoretical, since it's actually done by the renderer. According to the source of the Bitmap class, it's just a hint, and the renderer may or may not honor it.

这有点理论化,因为它实际上是由渲染器完成的。根据 Bitmap 类的来源,这只是一个提示,渲染器可能会也可能不会兑现。

/**
 * Set a hint for the renderer responsible for drawing this bitmap
 * indicating that it should attempt to use mipmaps when this bitmap
 * is drawn scaled down.
 *
 * 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.
 *
 * This property is only a suggestion that can be ignored by the
 * renderer. It is not guaranteed to have any effect.
 *
 * @param hasMipMap indicates whether the renderer should attempt
 *                  to use mipmaps
 *
 * @see #hasMipMap()
 */
public final void setHasMipMap(boolean hasMipMap) {
    nativeSetHasMipMap(mNativeBitmap, hasMipMap);
}

I'm not quite sure why this would be especially suitable for application icons, though. Although Android on tablets, as well as some launchers (e.g. GEL), request an icon "one density higher" to show it bigger, this is supposed to be done using the regular mechanism (i.e. drawable-xxxhdpi, &c).

不过,我不太确定为什么这特别适合应用程序图标。尽管平板电脑上的 Android 以及一些启动器(例如 GEL)要求图标“高一个密度”以显示更大,但这应该使用常规机制(即drawable-xxxhdpi,&c)来完成。

回答by kreker

One thing I mentioned in another thread that is worth pointing out -- if you are building different versions of your app for different densities, you should know about the "mipmap" resource directory. This is exactly like "drawable" resources, except it does not participate in density stripping when creating the different apk targets.

我在另一个线程中提到的一件事值得指出——如果您为不同的密度构建不同版本的应用程序,您应该了解“mipmap”资源目录。这与“可绘制”资源完全一样,只是它在创建不同的 apk 目标时不参与密度剥离。

https://plus.google.com/105051985738280261832/posts/QTA9McYan1L

https://plus.google.com/105051985738280261832/posts/QTA9McYan1L

回答by Stev

Since I was looking for an clarifying answer to this to determine the right type for notification icons, I'd like to add this clear statement to the topic. It's from http://developer.android.com/tools/help/image-asset-studio.html#saving

由于我正在寻找对此的澄清答案以确定通知图标的正确类型,因此我想在该主题中添加此明确声明。它来自http://developer.android.com/tools/help/image-asset-studio.html#saving

Note: Launcher icon files reside in a different location from that of other icons. They are located in the mipmap/ folder. All other icon files reside in the drawable/ folderof your project.

注意:启动器图标文件位于与其他图标不同的位置。它们位于 mipmap/ 文件夹中。所有其他图标文件都位于项目的 drawable/ 文件夹中。

回答by Fartab

When building separate apks for different densities, drawable folders for other densities get stripped. This will make the icons appear blurry in devices that use launcher icons of higher density. Since, mipmap folders do not get stripped, it's always best to use them for including the launcher icons.

为不同密度构建单独的 apk 时,其他密度的可绘制文件夹会被剥离。这将使图标在使用更高密度的启动器图标的设备中显得模糊。由于 mipmap 文件夹不会被剥离,因此最好使用它们来包含启动器图标。

回答by Hayk Nahapetyan

There are two cases you deal with when working with images in Android:

  1. You want to load an image for your device density and you are going to use it “as is”, without changing its actual size. In this case you should work with drawables and Android will give you the best fitting image.
  2. You want to load an image for your device density, but this image is going to be scaled up or down. For instance this is needed when you want to show a bigger launcher icon, or you have an animation, which increases image's size. In such cases, to ensure best image quality, you should put your image into mipmap folder. What Android will do is, it will try to pick up the image from a higher density bucket instead of scaling it up.

SO

Thus, the rule of thumb to decide where to put your image into would be:

  1. Launcher icons always go into mipmap folder.

  2. Images, which are often scaled up (or extremely scaled down) and whose quality is critical for the app, go into mipmap folder as well.

  3. All other images are usual drawables.

在 Android 中处理图像时,您会遇到两种情况:

  1. 您想为您的设备密度加载图像,并且您将“按原样”使用它,而不更改其实际大小。在这种情况下,您应该使用可绘制对象,Android 将为您提供最合适的图像。
  2. 您想为您的设备密度加载图像,但该图像将被放大或缩小。例如,当您想显示更大的启动器图标时,或者您有动画,这会增加图像的大小时,就需要这样做。在这种情况下,为了确保最佳图像质量,您应该将图像放入 mipmap 文件夹。Android 会做的是,它会尝试从更高密度的存储桶中提取图像,而不是将其放大。

所以

因此,决定将图像放入何处的经验法则是:

  1. 启动器图标总是进入 mipmap 文件夹。

  2. 图像通常被放大(或极度缩小)并且其质量对应用程序至关重要,也会进入 mipmap 文件夹。

  3. 所有其他图像都是通常的可绘制对象。

Citation from thisarticle.

引文从这个文章。

回答by Evan

If you build an APK for a target screen resolution like HDPI, the Android asset packageing tool,AAPT,can strip out the drawables for other resolution you don't need.But if it's in the mipmap folder,then these assets will stay in the APK, regardless of the target resolution.

如果你为 HDPI 等目标屏幕分辨率构建 APK,Android 资产打包工具 AAPT 可以剥离出你不需要的其他分辨率的可绘制对象。但如果它在 mipmap 文件夹中,那么这些资产将保留在APK,无论目标分辨率如何。

回答by Piyush

res/
mipmap-mdpi/ic_launcher.png (48x48 pixels)
mipmap-hdpi/ic_launcher.png (72x72)
mipmap-xhdpi/ic_launcher.png (96x96)
mipmap-xxhdpi/ic_launcher.png (144x144)
mipmap-xxxhdpi/ic_launcher.png (192x192)

MipMap for app icon for launcher

用于启动器应用程序图标的 MipMap

http://android-developers.blogspot.co.uk/2014/10/getting-your-apps-ready-for-nexus-6-and.html

http://android-developers.blogspot.co.uk/2014/10/getting-your-apps-ready-for-nexus-6-and.html

https://androidbycode.wordpress.com/2015/02/14/goodbye-launcher-drawables-hello-mipmaps/

https://androidbycode.wordpress.com/2015/02/14/goodbye-launcher-drawables-hello-mipmaps/