Wednesday, March 18, 2015

Mipmap in android

WHY Mipmap:

Android Devices are having different resolutions of hdpi, mdpi xhdpi xxhdpi and tvdpi. The app icon is decided by the Launcher application. Launchers use getDrawableForDensity and scale down if needed, rather than up, so the icons are high quality.

All Android developers face problems in Magnification of images, while magnification of images in Android, we face the texels of the image are easily visible, as they now cover many of the pixels on our display. With minification, many of the details are lost as many of the texels cannot be rendered onto the limited pixels available. To avoid this, some developers used this “mipmaps”.

On MDPI device Launcher might want the larget HDPI icon to display the large shortcuts.How to do this?
by Mipmap image set. so we can say, Displaying the larger App shortcuts in a low resolution device we used Mipmap image drawables in android.

What is MipMap:

"In 3D computer graphics, mipmaps (also MIP maps) are pre-calculated, optimized collections of images that accompany a main texture, intended to increase rendering speed and reduce aliasing artifacts. They are widely used in 3D computer games, flight simulators and other 3D imaging systems for texture filtering. Their use is known as mipmapping. The letters “MIP” in the name are an acronym of the Latin phrase multum in parvo, meaning “much in little”.[1] Since mipmaps cannot be calculated in real time, additional storage space is required to take advantage of them. They also form the basis of wavelet compression."

"This is exactly like "drawable" resources, except it does not participate in density stripping when creating the different apk targets."


Mipmaps are used for:

1 - Speeding up rendering times (smaller textures equate to less memory usage);
2 - Improving the quality. Rendering large textures where only small subsets of points are used can easily produce moiré patterns;
3 - Reducing stress on GPU.

How Mipmap works in android?

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.

 Another 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.

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.

How can we minify textures without introducing noise and use all of the texels? This can be done by generating a set of optimized textures at different sizes which we can then use at runtime. Since these textures are pre-generated, they can be filtered using more expensive techniques that use all of the texels, and at runtime OpenGL will select the most appropriate level based on the final size of the texture on the screen.

android-mipmap

The resulting image can have more detail, less noise, and look better overall. Although a bit more memory will be used, rendering can also be faster, as the smaller levels can be more easily kept in the GPU’s texture cache.

Let’s take a closer look at the resulting image at 1/8th of its original size, using bilinear filtering without and with mipmaps; the image has been expanded for clarity:

Untitled-1

The version using mipmaps has vastly more detail. Because of the pre-processing of the image into separate levels, all of the texels end up getting used in the final image.

Where Should We use

Mipmaps are able to work with Android 2.2+. But with Android 4.3, Google almost forces developers to use mipmaps with all their apps. This will dramatically Speeding up rendering times of images, Improving the quality and Reducing stress on GPU. And with this, developers can use heavy graphics and textures without any second thinking..