what does android getIntrinsicHeight and getIntrinsicWidth mean?

Hi I am confused by the two methods from Android Drawable class

getIntrinsicHeight()
getIntrinsicWidth()

api definition says http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getIntrinsicHeight()

what does the word intrinsic height/width mean? i mean is it a width of the actual image?


If you want to know the meaning of intrinsic, it is nothing but the actual property possessed by an object. In our case getIntrinsicWidth/Height simply means to provide you with the default width/height of that drawable.

This returns the exact size of the drawable which you have put in the resource folder without any modification.

Now you have to know that getWidth or getHeight will return a value which might vary according to the width and height you specify for your ImageView in your XML layout.

Let's say that you have provided the width and height of your ImageView as 100*100 in the XML layout and the drawable you used as the background is of size 200*200.

Now getIntrinsicWidth must return 200 whereas getWidth must return 100.


related question here on stackoverflow.

IF your image is downloaded from the internet, .getIntrinsicWidth() and .getIntrinsicHeight() indeed give you the "real" width and height, respectively of the image.

It's called intrinsic, because it depends ONLY on the image and on nothing else (such as your phone).

Alas, what you get is NOT intrinsic in all circumstances - it DOES depend things other than the image, unfortunately.

Here is where you get a wrong (namely, non-intrinsic) result. Let's say you are using the default launcher icon, then

Log.i("", "ic_launcher intrinsic width " + getResources().getDrawable(R.drawable.ic_launcher).getIntrinsicWidth());

will tell you the width (in pixels) of the launcher icon. But of which one? - you have several of them, one in drawable-xhdpi folder, one in drawable-hdpi folder, etc. Well, if your device is, say, xhdpi, it gives you 96, which is indeed the pixel-width of the version of the launcher icon residing in the drawable-xhdpi folder. Now, delete the icon in the drawable-xhdpi folder, and run again (still using an xhdpi device (real or emulated)). The image that will be used will be from drawable-hdpi folder, because that's "closest" to the xhdpi version. That icon has a pixel width of 72. But above code WILL STILL GIVE YOU 96!!!

That is clearly NOT "intrinsic" (in the proper sense of the word), as it does not depend only on the image used.

So if you are as lazy as I am, and are therefore not generating 4 versions of each resource icon/image (but instead using only 1 or 2, and scaling them by hand), you have to beware the mentioned androidal misnomer.