Search code examples
androidandroid-image

Relationship between image resources and their resultant size on screen


Say I have a cat.png in my drawable-hdpi folder, with dimension 100*100 px. Now if I load this picture using an ImageView whose size is set to "wrap_content":

cat_image.setImageResource(R.drawable.cat);

How can I know the size of the image view (in pixels) without calling "getHeight()"? There must be a relationship between the size of image view and the number "100", but I tried to convert "100" from DP to PX, and from PX to DP, etc, with no success.


Solution

  • if a image is 100px that in mdpi is 100dp.

    Then something that is 100dp means: its 100px on mdpi its 150px on hdpi its 200px on xhdpi

    So android will try to find the best suited resource to use. for drawables it also scales the image automatically so that if you have it as 100px and placed in mdpi it will use it for hdpi screens but make it 150px. This is not recommended and you should always try to have all the resources into all of the folders (mdpi hdpi xhdpi).

    There is a helper method that can help you convert 100dp into pixels through code.

    Converting pixels to dp