androidandroid-imageviewandroid-imageandroid-screen-support

Android images for different screens


I'm developing an Android app which uses a number of images and icons. In order to prevent the device to work hard and run out of memory, I want to create multiple images for each screen size.
My question is: Do I need to measure exactly what dp I want and then convert it to pixels?
Sometimes it's hard to know exactly the size of the image in dp- for instance if it's width is match_parent and there's margin etc.

How can I solve this problem?
I would be glad for guidance on how to do this the best way.


Solution

  • Do not create images for each screen size, but rather for each screen density. So you will need five different files for mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi (if you don't want to support some of these, just don't put the file in the corresponding folder). The algorithm is the following: take your image and put it to the 'xxxhdpi' folder. Then resize it, so it looks as you want on a device with the xxxhdpi density screen. Then resize your image for other densities using the following ratios:

    mdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
    1    | 1.5  | 2     | 3      | 4
    

    For example, to get the size of your mdpi image, divide a width and a height of the xxxhdpi image by four.