Image Size Doubling - android

This might be a quick answer but I must not know what to search for as I can't find anyone talking about it! I have some png files in my drawables folder that are 88x88 pixels. I run my app and have a log print out the size of the images and it claims they are 176x176. What gives? Is something expanding my images before they go into the apk or is my phone really only half the resolution it claims?

BitmapFactory.decodeResource scales images in the Drawable folder according to the DPI of the device (it generates a higher resolution image for high resolution screens).
If you don't want the image to be scaled on decoding, place it in the folder Drawable-nodpi

From this guide:
"By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities."
So yes, your screen is probably xhdpi with density 2, so size of your image from unspecified drawables folder would be x2.

Related

One PNG image for all densities

Can I use just one png image big enough for all densities? Or do I need to create one png image for every density?
I tries placing just one png image in drawable folder but it becomes blurry. So I placed that same image in the drawable-xxhdpi (my test device density) and it shows correctly (clear). I guess that the blurry effect is because it is multiplying for the scale factor of the screen density. The idea should be to say to Android to take the big image (xxxhdpi) and make it smaller (never bigger).

Do I need to use all of drawable, drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi & drawable-xxxhdpi?

I realise that Android will try it's best if it does not find an icon in the required folder but I see someplaces they suggest all of the above and in others they don't include drawable ?
So should I populate drawable as well if all the others are filled with my tab icon images?
If you use a VectorDrawable, you don't need to add a Resource for every density (mdpi, xhdpi etc). VectorDrawable is supported since API 21 (Lollipop) or with Support Library (or AndroidX).
For simple types of images (usually icons), you can avoid creating separate images for each density by using vector graphics. Because vector graphics define the illustration with geometric line paths instead of pixels, they can be drawn at any size without scaling artifacts.
For images (PNG) on the other hand, you must add proper icons for every density because Android will try to scale the images (so they can proportionally occupy same area in all devices). When scaling, the image may become blurred reducing the quality of your UI.
To provide good graphical qualities on devices with different pixel densities, you should provide multiple versions of each bitmap in your app—one for each density bucket, at a corresponding resolution. Otherwise, Android must scale your bitmap so it occupies the same visible space on each screen, resulting in scaling artifacts such as blurring.
You can read more HERE and HERE
EDIT
Maybe, you don't need to duplicate ALL icons. A lot of factors can lead to different experiences such as using wrap_content or a specific dimension to control the icon size or even using a different scaleType in your ImageView. So, maybe, you can start by adding icons for xhdpi or xxhdpi folders only and check your screen in different screen (small display, large displays, low-resolution displays, high-resolution displays etc). Then, you can "duplicate" only the necessary icons... But if your project or APK size is relatively small, don't mind to duplicate the icons.
There's even some online tools to generate the assets for every density from a single PNG such Android Asset Studio website..
If you are adding all resolution drawable icon like :
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxdpi
then you need not to add any extra icons on drawable folder
because all the device resolution covers under the above drawable folders

Detemine correct image size to use for nodpi

I had different nine-patch images for different screens. However, I encountered an issue
android drawable image takes lot of memory compared to its disk size
So, I decided to use, for now, nodpi with hdpi image size.
Since, I have limited understanding, I was wondering what should be the image size I should put in drawable-nodpi folder.
Should I put hdpi images or xxxhdpi images? Or something else?
Please help me understand.

Why the need for drawable folders?

I have always noticed that i have many drawable directories - drawable hdpi, ldpi, mdpi, xhdpi. I know that these directories are for different screen densities(scaling up purposes, like android will use an image from hdpi if your screen has that screen density. But isnt that the whole point of the unit dp? Why invent the wheel again?
When you describe a view size with dp units, you're only saying how much space it should take up on the screen. In the case of of a drawable, the renderer will scale the raw image up or down to fit your dimensions. The reason for the different drawable folders is to optimize for different resolutions.
On a screen device with higher pixel density, the interpolation performed by the renderer will make a low resolution raw image look blurry, which is why we need high resolution assets.
On a screen with low pixel density, the renderer can and will scale down high resolution images when low resolution ones aren't present, but this is wasteful. First of all, the interpolation performed by the renderer might give you an ugly result. Secondly, the raw image is decoded at full resolution only to be displayed in a view of a much smaller size. Thats wasting CPU cycles and memory, both of which tend to be less plentiful on devices with low pixel densities.

Android drawable folders system

so, I've created a project where I use an image in the action bar, but android only scaled it to the folders -mdpi, -hdpi, -xhdpi, -xxhdpi, why isn't it scaling to the low density folder ?
Btw, what if I want to use an image that is going to be the same size no matter what screen resolution is being used, should I create a folder just for this images ?
I believe they removed ldpi because there are so few ldpi devices out and they can make use of mdpi anyway.
Having an image the same size on any resolution is what these folders are for!! If you put an image in the static drawable folder its size will be different depending on what device you use because the pixel density is the same in the picture but it has to be scaled differently on screen to adjust for the larger or smaller density of the screen
go read the introductory android tutorials to learn more about screen density

Categories

Resources