What is the advantage of the multiple available?

Most of the resources indicated that when using (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi), android does not need to scale the image and we will not have the CPU overhead to rescale the image.

but I haven't worked so far, suppose I have an icon image with all the dimensions already mentioned (36px, 48px, 72px, 96px, 144px and 192px), well, in the code below you can see that I am using 40dp, that it is not fits whatever dimensions, so android will resize the image again, what is the advantage if multiple drawable?

<ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android: src="@drawable/icon"
        />

      

+3


source to share


2 answers


You will often not have an exact pixel match image for the DP that you describe in the layout - as in the case described above. When that happens, Android still has to scale up a bit.

The advantage is not CPU savings, but memory savings. Scaling operations on large images can be memory intensive, especially when scaling from small to large or large to small.

If you start with a close image and only need to scale up a bit, the memory cost is less significant.



If the image is w500px, h500px, that is 250,000 pixels. Even though this image is a jpg and compressed to 10KB on disk, the scaling requires Android to convert to RGB or ARGB, which is 3 or 4 bytes per pixel.

4 bytes x 250,000 = 1M memory. 500 pixels on a device, which is XXXHDPI, is only about 1 inch on screen. So you can see how it can stack up quickly.

EDIT: This might be helpful https://developer.android.com/topic/performance/graphics/load-bitmap.html

0


source


Contrary to what Aaron says, the benefit of providing these images is not system resources, but how your app will look to users across devices. By providing high-resolution copies of the same image, users on high-resolution devices will see crisp, crisp images instead of scaled, fuzzy images.

Your question mentions specific pixel sizes for an image. I assume these values ​​are taken from https://developer.android.com/guide/practices/screens_support.html

  • 36x36 (0.75x) for low density
  • 48x48 (baseline 1.0x) for medium density
  • 72x72 (1.5x) for high density
  • 96x96 (2.0x) for ultra high density
  • 144x144 (3.0x) ultra high density
  • 192x192 (4.0x) for super extra super high density

When you provide all these different resolutions for your image, you are not providing different physical dimensions . Of course, 144x144 is three times the size of 48x48, but this 144x144 image won't display three times as large when the user sees it on their device.

The system will only display a 144x144 image when the user has a device with a high resolution screen. This means that the individual pixels on their display will be smaller, which means that a 144x144 image will take up the same physical space as a 48x48 image on a lower resolution screen.

In short: all images in this example are 48dp x 48dp.

Now if you render this image in ImageView

, which is 40dp x 40dp, the image will either be cropped or scaled down. But that has nothing to do with the additional images you provided. On a very low resolution screen or a very high resolution screen, the system will still stuff the 48x48 image into a 40x40 container.

Update

Here's an example. I used Android Studio menu New -> Image Asset

to create an image. You can see that AS is generating multiple copies of the same image with different sizes:



enter image description here

Then I create two emulators:

And then I run an application that just displays my image on both of them:

You can see that the status bars, action bars and the little android are all the same size. But the Nexus S displays an hdpi image (48px x 48px), while the Nexus 5 displays an xxhdpi image (96px x 96px). Both look sharp!

enter image description here

Finally, I delete all copies of my image except the mdpi. Now both emulators are using the mdpi image, but you can see that they are still displayed at the same size. The internal image size is 32dp x 32dp, so while both emulators use a 32px x 32px image, both must scale it (Nexus S must scale it 1.5x, and Nexus 5 must scale it 3x).

enter image description here

As you can see, the small android is the same size, but it looks much worse . This is why you provide copies of the same image at different resolutions: so your images always appear sharp!

-1


source







All Articles