70

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?

user1697965
  • 1,255
  • 3
  • 14
  • 22
  • 2
    correct answer here http://stackoverflow.com/questions/6536418/android-why-are-the-width-height-of-the-drawable-in-imageview-wrong – mathheadinclouds Jan 26 '15 at 21:46

3 Answers3

155

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.

Anas Azeem
  • 2,820
  • 3
  • 24
  • 37
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • thanks i see. but currently i am downloaidng an image from url (and load it ot an image view) the intrinsic height and width is not same as the original image if i access it in browser, it is biger than what is being reported, but in ur above example, u r saying they should be the same – user1697965 Dec 07 '12 at 05:34
  • well maybe you are scaling the image while downloading.I am not sure how far this works for a downloaded image but at least it works for those images which I have in my drawable. – Andro Selva Dec 07 '12 at 05:36
  • 11
    wrong. correct answer here http://stackoverflow.com/questions/6536418/android-why-are-the-width-height-of-the-drawable-in-imageview-wrong – mathheadinclouds Jan 26 '15 at 21:11
  • 4
    WARNING - intrinsicHeight differs depending on the screen density and cannot be used as a reliable measure of getting actual source image dimensions - see the link provided by @mathheadinclouds Of course, the Android docs never state this - leading to millions of wasted hours by developers worldwide. – RunLoop Sep 02 '15 at 06:22
  • Wrong. This gives a number that depends on the screen density. – auspicious99 Feb 07 '20 at 06:26
13

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.

Community
  • 1
  • 1
mathheadinclouds
  • 3,507
  • 2
  • 27
  • 37
4

In android a drawable can be of many types such as color, bitmap, shape etc.

Some of these drawables have an intrinsic height such as a BitmapDrawable which is the dimension of the image.

Drawables such as ColorDrawable (used to draw just solid colors) don't have an intrinsic height. In this case the value of getIntrinsicHeight/Width returns -1.

Even if a drawable doesn't have intrinsic height/width, every drawable needs to have their bounds set before they can render itself (i.e before you call mydrawable.draw(canvas))

If you using a drawable as a background for a view, the view internally sets the bounds for you. But if you are using drawables in your own onDraw, then you need to explicitly set the bounds via setBounds.

numan salati
  • 19,394
  • 9
  • 63
  • 66