what does android getIntrinsicHeight and getIntrinsicWidth mean?

前端 未结 3 397
旧巷少年郎
旧巷少年郎 2020-12-23 13:19

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

getIntrinsicHeight()
getIntrinsicWidth()

api definition says http://develo

相关标签:
3条回答
  • 2020-12-23 13:45

    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.

    0 讨论(0)
  • 2020-12-23 13:55

    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.

    0 讨论(0)
  • 2020-12-23 14:01

    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.

    0 讨论(0)
提交回复
热议问题